dtyw10299 2017-11-06 05:35
浏览 101
已采纳

PHP-按特定顺序显示结果

I need my booking system to display bookings in order from the earliest time. So if someone booked an equipment from 12:00 to 2:00, then the next person to use the system books the same equipment on the same day from 10:00 to 11:00. Following this a third person books the same equipment on the same day from 11.00 to 12.00.

The booking system works fine, but displays it in the order of bookings. So with the above example, the starting times would show up as:

-12.00 -10.00 -11.00

What I would like to do is display the starting times in order of earliest. So the above example would be displayed as following:

-10.00 -11.00 -12.00

 $sqlEvent="select * from bookings where selected_date='".$month."/".$day."/".$year."'";
 $resultEvents=mysqli_query($dbconnection,$sqlEvent);
 echo "<br>";

 while($events=mysqli_fetch_array($resultEvents)){

 echo "<span class='one'><span class='oneagain'>Equipment:</span> ".htmlspecialchars($events['equipment'])."</span><br>"; 
 echo "<span class='two'><span class='twoagain'>Start Time:</span> ".htmlspecialchars($events['start_time'])."</span><br>";
 echo "<span class='three'><span class='threeagain'>End Time:</span> ".htmlspecialchars($events['end_time'])."</span><br>";

 }

}

I tried the following, but nothing worked:

$sqlEvent="select * from bookings ORDER BY start_time ASC where selected_date='".$month."/".$day."/".$year."'";
  • 写回答

1条回答 默认 最新

  • dptgpyl61857413 2017-11-06 05:45
    关注

    You need to rethink the structure of your database. Your field selected_date should not be stored as a string like "MM/DD/YYYY", but should be stored in one of the native date formats like UNIX timestamp or DATETIME. Then, you can sort by that field properly and reformat it on the way in and out of the database. So something like:

    "SELECT *, DATE_FORMAT(selected_date, '%m/%d/%Y') as formatted_date FROM BOOKINGS WHERE DATE(selected_date)='$year-$month-$day' ORDER BY selected_date ASC"
    

    This would give you your ordered results and formatted_date as the nicely displayed date. You'll need to modify this to fit your actual code and structure, but this is the way to query dates if you need to do any date-related work (like sorting or comparing dates).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图