2 回答

TA貢獻1856條經(jīng)驗 獲得超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條經(jīng)驗 獲得超8個贊
定義一個常量/數(shù)組來與數(shù)組進行比較,并使用鍵值將星期幾設置為從星期日開始。然后使用該常量/數(shù)組運行 foreach 來回顯從周日開始的每一天的表數(shù)據(jù)。檢查常量中的值是否在返回的數(shù)組中,如果是,則回顯該天及其也設置為該天的數(shù)組中的相應值。
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;
根據(jù)您的情況,請嘗試以下操作:
// 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 關(guān)注
- 157 瀏覽
添加回答
舉報