2 回答
TA贡献1856条经验 获得超11个赞
这是我的完全有效的代码:
$WEEKDAYS = array(1 => "Sunday", 2 => "Monday", 3 => "Tuesday", 4 => "Wednesday", 5 => "Thursday", 6 => "Friday", 7 => "Saturday");
$ResultsArray = array();
while($row = mysqli_fetch_array($result)) {
// create an array to hold the return values from your DB
$date = $row['dia_semana']; //<--- coming from db holds date values
$ResultsArray[$date][] = $row['temperature'];
}
echo "<table style='padding:5px;border-radius: 10px;border:solid 1px #000;vertical-align: text-top;'>";
echo "<tr>";
foreach ($WEEKDAYS as $key => $date) {
echo "<th>".$date."</th>";
}
echo "</tr>";
for ($x = 0; $x < 1; $x++){
echo "<tr>";
echo "<td>".$ResultsArray['Sunday'][$x]."</td>";
echo "<td>".$ResultsArray['Monday'][$x]."</td>";
echo "<td>".$ResultsArray['Tuesday'][$x]."</td>";
echo "<td>".$ResultsArray['Wednesday'][$x]."</td>";
echo "<td>".$ResultsArray['Thursday'][$x]."</td>";
echo "<td>".$ResultsArray['Friday'][$x]."</td>";
echo "<td>".$ResultsArray['Saturday'][$x]."</td>";
echo "</tr>";
}
echo "</table>";
TA贡献1844条经验 获得超8个赞
定义一个常量/数组来与数组进行比较,并使用键值将星期几设置为从星期日开始。然后使用该常量/数组运行 foreach 来回显从周日开始的每一天的表数据。检查常量中的值是否在返回的数组中,如果是,则回显该天及其也设置为该天的数组中的相应值。
define("WEEKDAYS", [1 => "Sunday", 2 => "Monday", 3 => "Tuesday", 4 => "Wednesday", 5 => "Thursday", 6 => "Friday", 7 => "Saturday"]);
$parseDays = array(
'Frank' => 'Monday',
'Jean' => 'Monday',
'Mike' => 'Sunday',
'Bob' => 'Tuesday',
'Bill' => 'Friday',
'Jack' => 'Sunday',
'George' => 'Friday',
'Dillon' => 'Wednesday'
);
$stmt = '<table>';
$stmt .= '<tr>';
foreach(WEEKDAYS as $key => $day){
if(in_array($day, $parseDays)){
if($day === $day){
$stmt .= '<td style="padding:5px;border-radius: 10px;border:solid 1px #000;vertical-align: text-top;">'.$day.'<hr>';
foreach($parseDays as $name => $days){
if($days === $day){
$stmt .= '<br>'.$name;
}
}
$stmt .= '</td>';
}
}
}
$stmt .= '</tr>';
$stmt .= '</div>';
echo $stmt;
根据您的情况,请尝试以下操作:
// make sure to define a constant that has the days to compare so we can start
// with Sunday and display in order of week from Sunday through the rest of
// the week concurrently
define("WEEKDAYS", [1 => "Sunday", 2 => "Monday", 3 => "Tuesday", 4 => "Wednesday", 5 => "Thursday", 6 => "Friday", 7 => "Saturday"]);
while($row = mysqli_fetch_array($result)) {
// create an array to hold the return values from your DB
$date[] = $row['dia_semana']; //<--- coming from db holds date values
}
// construct the table and display the days
$stmt = '<table>';
$stmt .= '<tr>';
//loop through the constant and get the days in order from Sunday concurrently
foreach(WEEKDAYS as $key => $day){
// check if the $day value from constant is located in the $date array from db
if(in_array($day, $date)){
if($day === $day){
$stmt .= '<td style="padding:5px;border-radius: 10px;border:solid 1px #000;vertical-align: text-top;">'.$day.'<hr>';
// loop through db array and assign key/values
foreach($date as $name => $days){
// If value from db array is equal to value from constant, display it
if($days === $day){
$stmt .= '<br>'. //enter name values here;
}
}
$stmt .= '</td>';
}
}
}
$stmt .= '</tr>';
$stmt .= '</div>';
echo $stmt;
输出:
- 2 回答
- 0 关注
- 107 浏览
添加回答
举报