Php 日历中的mysql数据检查是否已预订

Php 日历中的mysql数据检查是否已预订,php,arrays,calendar,multidimensional-array,Php,Arrays,Calendar,Multidimensional Array,我有一个数据库,里面有物品的预订。所有预订都有一个:ID、item ID、personwhobokedid、time_from(unixtime)、time to(unixtime) 我喜欢一小时一小时地显示每天的时间安排,并为预订项目的时间着色,并显示在该时间范围内预订的人员 比如: room1: 5:00 - 6:00: free 6:00 - 7:00: booked by[person] 8:00 - 9:00:free 及 等 此时,我使用mysql遍历现有的项目,然后使用mysql

我有一个数据库,里面有物品的预订。所有预订都有一个:ID、item ID、personwhobokedid、time_from(unixtime)、time to(unixtime)

我喜欢一小时一小时地显示每天的时间安排,并为预订项目的时间着色,并显示在该时间范围内预订的人员

比如:

room1:
5:00 - 6:00: free
6:00 - 7:00: booked by[person]
8:00 - 9:00:free

此时,我使用mysql遍历现有的项目,然后使用mysql检查每个时间段是否有一个等于itemID和Times的预订,该预订介于所提到的时间段from和time to之间

这是可行的,但会对数据库进行大量查询

为了提高性能,我认为我最好首先获取所有预订并将其存储在多维数组中,如下所示:

     $allreservationsperday = mysql_query("select id, personid, itemid, time_from, time_to FROM reservations where time_from >= '$unixfrom' and time_to < '$unixto'");

 while($reservationarray = mysql_fetch_array($allreservationsperday)){
 $res[$reservationarray ["id"]]["personid"] = $reserveringenarray["personid"];
 $res[$reservationarray ["id"]]["itemid"] = $reserveringenarray["itemid"];
 $res[$reservationarray ["id"]]["time_from"] = $reserveringenarray["time_from"];
 $res[$reservationarray ["id"]]["time_to"] = $reserveringenarray["time_to"];
}
$allreservationsperday=mysql_查询(“选择id、personid、itemid、time_from、time_to from预订,其中time_from>='$unixfrom'和time_to<'$unixto');
而($reservationarray=mysql\u fetch\u array($allreservationsperday)){
$res[$reservationarray[“id”][“personid”]=$ReservingArray[“personid”];
$res[$reservationarray[“id”][“itemid”]=$reservingenarray[“itemid”];
$res[$reservationarray[“id”][“time_from”]=$reserveringarray[“time_from”];
$res[$reservationarray[“id”][“time_to”]=$reserveringarray[“time_to”];
}
然后通过for循环显示时间线,以循环方式显示一天中的小时数 但是我不知道如何每小时检查刚刚形成的数组中是否有保留

欢迎任何帮助!
jeroen

只需在它们之间循环,并检查行中下一个值的差异

foreach($res as $key=>$val){
    if($val['time_from'] == $res[$key+1]['time_from'])
        //this is same hour. lets do something here?
    else
        //this is a new hour. 
}
当然,在比较之前,您必须检查$res[$key+1]是否也已设置

约纳兹: 我看不出如何将此融入脚本中。 目前,我尝试了以下方法(了解船只租赁概述): 下面的脚本预览位于

echo“
今天的概述:
”; $boten=getBootIdType(“2”)//把所有能租的船都租下来 foreach($boten as$boten=>$value){//显示每艘船的时间线 回声“; 回声“船”.$value.“; $unix_from=strotime(日期(“d-m-Y 7:00”);//预订时间为上午7点至晚上9点 $unix_to=strotime(日期(“d-m-Y 21:00”);
来自@Jeroen的($unix_)-可能与您相关的几个“预订系统”帖子:和
foreach($res as $key=>$val){
    if($val['time_from'] == $res[$key+1]['time_from'])
        //this is same hour. lets do something here?
    else
        //this is a new hour. 
}
    echo "<br>Overview for today:<br><table cellpadding=0 cellspacing=0 border=1 class='navbar' id='navbar' width='100%'>";
$boten = getBootIdType("2"); //Get all boats that can be rented
foreach($boten as $boten=>$value){ //display a timeline per boat
    echo "<tr>";
    echo "<td>Boat ".$value."</td>";

    $unix_from = strtotime(date("d-m-Y 7:00")); //bookings can be made from 7am to 9pm
    $unix_to = strtotime(date("d-m-Y 21:00"));

    while ($unix_from <= $unix_to) {

     // HERE I WANT TO CHECK IF A BOOKING EXISTS IN THE ARRAY AND DISPLAY THE PERSON ID AS IDENTIFIER
     // and set a different background is there is a booking on that time

        echo "<td bgcolor='$bg'>".date("H:i", $unix_from)."</td>";
        $unix_from = $unix_from + 1800;
    }
    echo "</tr>\r\n";
}
echo "</table>";