2010-01-26 61 views
0

我有一個數據庫與物品保留。所有預訂都有:ID,物品ID,personWhoBookedId,time_from(unixtime),time-to(unixtime)。mysql數據從日曆檢查,如果預訂

我喜歡每小時顯示一天的時間安排,併爲該項目預訂的時間上色,並在該時間範圍內顯示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和時間 - from和time-到

這可以工作,但是會在數據庫上做很多查詢。

爲了提高性能,我想我最好弗里斯特得到全部保留,並將它們,存儲在一個多維數組是這樣的:

 $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

+0

@Jeroen - 你一對夫婦可能與「預訂系統」的帖子的: HTTP:/ /stackoverflow.com/questions/1692969/booking-system-dates-in-database/1693079#1693079和http://stackoverflow.com/questions/1687519/searching-for-availability-with-mysql-and-php/1691282 #1691282 – 2010-01-26 23:35:40

回答

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]是否在比較之前也設置。

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