1

我正在制作一个时间表页面来展示一些节目。我想要正确的 PHP 算法根据节目开始时间显示节目。现在什么都没有显示。

这是一个链接:http ://tir.fullerton.edu/schedule/schedule.php

这是我的代码:

  $result = mysqli_query($con,"SELECT * FROM schedule
ORDER BY FIELD(showDay, 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY'),
showStart");
echo "<br>";
$i = 8;
while($row = mysqli_fetch_array($result))
{
if ( $row['showDay'] == 'Monday')
{

    if ($row['showStart'] == $i)
    {
    echo '<td>' . '<img class="newStyle1" src="images/'. $row['picURL'] . 
'"/>'. '</br> ' . $row['showName'] . ' </td>';
    }
    else
    {
    echo "<td></td>";
    }
    $i++;
 }

在此处安排 mysql 表:http: //tir.fullerton.edu/schedule.png

4

1 回答 1

0

不确定这是否是最好的方法,但这是我能想到的唯一方法......

<?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; 
   }
}  

** 我今天要走了...如果您有任何问题,请写一些评论,我明天会检查。

于 2013-10-07T22:19:48.597 回答