【问题标题】:Show Schedule Algorithm显示计划算法
【发布时间】:2013-10-07 21:30:18
【问题描述】:

我正在制作一个时间表页面来展示一些节目。我想要正确的 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

【问题讨论】:

  • schedule 的表格是什么样的?
  • 在文末查看餐桌安排截图
  • 你为什么要这样做? if ($row['showStart'] == $i)
  • showDay 是什么数据类型?
  • 我这样做是因为我想填写页面上的时间表 (schedule.php)。所以它从 8 点开始(表示上午 8:00)并继续进行,但我没有看到任何值被输出。 showday 是 varchar 数据类型

标签: php mysql database algorithm output


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 2015-03-10
    • 2013-04-26
    • 2013-04-27
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多