不确定这是否是最好的方法,但这是我能想到的唯一方法......
<?php
$con = new mysqli("localhost", "root", "password", "test");
$query = " SELECT PID, showName, showDay, showStart, showEnd, genre, picURL, showURL
FROM schedule
ORDER BY FIELD(showDay, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY'), showStart";
$result = mysqli_query($con,$query);
$data = array('Monday'=>array(),
'Tuesday'=>array(),
'Wednesday'=>array(),
'Thursday'=>array(),
'Friday'=>array());
// re-order the query results so they are easier to use
// ====================================================
while($row = mysqli_fetch_assoc($result))
{
$showDay = $row['showDay'];
$showStart = $row['showStart'];
$data[$showDay][$showStart] = $row;
}
?>
<table align="center" border="1">
<tr>
<th> Show Day </th>
<th> 8:00 AM </th>
<th> 9:00 AM </th>
<th> 10:00 AM </th>
<th> 11:00 AM </th>
<th> 12:00 PM </th>
<th> 1:00 PM </th>
<th> 2:00 PM </th>
<th> 3:00 PM </th>
<th> 4:00 PM </th>
<th> 5:00 PM </th>
<th> 6:00 PM </th>
<th> 7:00 PM </th>
<th> 8:00 PM </th>
<th> 9:00 PM </th>
<th> 10:00 PM </th>
</tr>
<?php
// foreach day....
// ===============
foreach($data as $day => $times)
{
echo "<tr><td><b>$day</b></td>";
// for every displayed hour
// ========================
for ($hour=8; $hour<23; $hour++)
{
// if data exists for this time slot, show it..
// ============================================
if (isset($times[$hour]))
{
$row = $times[$hour];
echo "<td>{$row['showName']}</td>";
}
// else show an empty cell
// =======================
else echo "<td></td>";
}
echo "</tr>"; // end the row
}
echo "</table>"; // end the table
?>
产生:
这是我的桌子:
+------+----------+---------+-----------+---------+-------+--------+---------+
| PID | showName | showDay | showStart | showEnd | genre | picURL | showURL |
+------+----------+---------+-----------+---------+-------+--------+---------+
| 1 | show 1 | Monday | 20 | 21 | NULL | NULL | NULL |
| 2 | show 2 | Monday | 13 | 21 | NULL | NULL | NULL |
| 3 | show 3 | Monday | 10 | 21 | NULL | NULL | NULL |
| 4 | show 4 | Monday | 22 | 21 | NULL | NULL | NULL |
+------+----------+---------+-----------+---------+-------+--------+---------+
并且....如果您需要显示某些内容长达三个小时或其他内容,请修改我重新排序的代码,如下所示:
while($row = mysqli_fetch_assoc($result))
{
$showDay = $row['showDay'];
$showStart = $row['showStart'];
$length = $row['showEnd'] - $row['showStart'];
for ($i=0; $i<$length; $i++) {
$data[$showDay][$showStart+$i] = $row;
}
}
** 我今天要走了...如果您有任何问题,请写一些评论,我明天会检查。