dongsuo0517 2011-06-07 21:56
浏览 7
已采纳

如何使用php和mysql在每天的每次更改中循环显示结果并显示一周中的某一天?

with my current query and loop:

$sched = mysql_query("SELECT * 
FROM  `shows` 
ORDER BY  `shows`.`show_time` ASC")
or die(mysql_error());  


echo "<ul>";

while($row = mysql_fetch_array($sched)){
echo "<li><a href=\"#$row[id]\">";
echo $row['title'];
echo "</li>";
}
echo "</ul>";

This works great for displaying my results like this:

  • Name of show 1
  • Name of show 2
  • Name of show 3

However, I want to add an item to the list at the beginning of every change in day so it would display as follows:

  • Monday
  • Name of show 1
  • Name of show 2
  • Tuesday
  • Name of show 3
  • Wednesday
  • Name of show 4

I can't quite wrap my brain around the loop needed to do this. It might be helpful to know that the field 'show_time' is a datetime type, so it has the information for both time and day of week.

Thanks.

  • 写回答

3条回答 默认 最新

  • dongtuan1980 2011-06-07 21:59
    关注

    Simple tweak:

    echo "<ul>";
    $curDay='';
    while($row = mysql_fetch_array($sched)){
       $d=date('l',strtotime($row['show_time']));
       if($d!=$curDay){
         echo '<li>'.$d.'</li>';
       }
       $curDay=$d;
       echo '<li><a href="#',$row['id'],'">',$row['title'],"</li>";
    }
    echo "</ul>";
    

    Initialize $curDay, and then each time through the loop, check to see if the particular day is different than the last time through the loop (or different from the initial value)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效