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

发布于 2024-08-19 06:15:38 字数 1247 浏览 4 评论 0原文

我有一个包含物品预订的数据库。所有预订都有: 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 循环显示时间线以循环一天中的各个小时 但我不知道如何每小时检查刚刚形成的数组中是否有预订。

非常欢迎任何帮助! 杰罗恩

I have a database with reservations for items. All reservations have an: ID , item ID, personWhoBookedId, time_from (unixtime) , time-to(unixtime).

I like to display a day per day time schedual from hour to hour, and colour the timethat an item is booked and display the personWhoBookedID on that timeframe.

like:

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

and

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

etc

At this time I iterate through the existing items with mysql, then check per timeframe with mysql if there is a booking that equals itemID and timeis between the mentioned timeframe of time-from and time-to

This works but is does a lot of queries on the database.

To increase performance I think I better frist get all the reservations and store them in a multi dimensional array, like this:

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

and then display the timeline by a for Loop to loop through the hours of the day
But I cannot figure out how to check per hour if there is a reservation in the just formed array.

Any help most welcome!
jeroen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

り繁华旳梦境 2024-08-26 06:15:38

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

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] 是否已设置。

just loop through them and check for differences on the next value in line?

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. 
}

of course you vill have to check if $res[$key+1] isset also before comparison.

哑剧 2024-08-26 06:15:38

@乔纳兹:
我不知道如何将其融入脚本中。
目前我尝试了这个(用于船舶租赁的概述):
以下脚本的预览位于此测试站点

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

@jonaz:
I don't see how to fit this into the script.
Currently I tried this (for an overview for boat rentals):
A preview of the script below is at 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>";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文