来自日历的mysql数据检查是否已预订

时间:2010-01-26 23:03:19

标签: php arrays calendar multidimensional-array

我有一个包含项目预订的数据库。所有预订都有:ID,商品ID,personWhoBookedId,time_from(unixtime),time-to(unixtime)。

我喜欢每小时显示一天的时间表,并为预定项目的时间着色,并在该时间范围内显示personWhoBookedID。

喜欢:

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

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

此时我使用mysql迭代现有项目,然后使用mysql检查每个时间帧是否有等于itemID和timeis的预订在上述时间段和时间之间的时间

这样可以,但对数据库进行了大量查询。

为了提高性能,我认为我更好地获取所有预订并将它们存储在多维数组中,如下所示:

     $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"];
}

然后通过for循环显示时间轴以循环播放当天的小时数 但我无法弄清楚如果在刚形成的阵列中有预约,每小时如何检查。

欢迎任何帮助! 的Jeroen

2 个答案:

答案 0 :(得分:0)

只是循环遍历它们并检查下一个值的差异?

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]。

答案 1 :(得分:0)

@jonaz: 我不知道如何将其纳入脚本。 目前我试过这个(关于船租赁的概述): 下面脚本的预览位于this test site

    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>";