繁体   English   中英

时间段预订脚本 - 检查可用性

[英]Time Slot Booking Script - Check availability

尝试建立一个基本的预订系统,用户可以说他们需要30分钟的预约,代码可以输出哪些时间段可以免费预订30分钟。

我首先尝试忽略“时间”因素,并通过“插槽”对其进行编码,以便将所有日期时间复杂度从中抽出,然后开始使其工作1天。

我将这一天划分为15分钟的插槽,然后为忙碌创建了$ slotstatus,为免费创建了0。

然后是一个简单的问题,查看表行,找到空闲插槽并回显它们。

我遇到的问题是所需时间超过1个插槽。 所以我需要计算“Slotstatus = 0”的数量,并确保有足够的可用时间来满足要求。

当我看到0时,我尝试使用count ++但是在我需要按顺序输出两个插槽的区域出现了问题。

我非常业余的代码和我这样做有点兴趣,以提高我的编码逻辑大脑是穷人。

请有人指出我正确的方向

“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“”“” “”“”“”“”“”“”“”“”“”“”“”“”“”

    $count = 0;
    $req = 3;
    echo $req . " is the SlotReq<br>";
    echo $count . " is the starting count<br><br>";




$sql = "SELECT slotnumber, slottime, slotstatus FROM timeslots";
$result = $conn->query($sql);


if ($result->num_rows > 0) 
    {
        // output data of each row
        while($row = $result->fetch_assoc() ) 
            {
                    if ($row["slotstatus"] == 0)
                    {
                        $count ++;

                            if ( $count == $req)
                            {


                                echo "<br>Pass for " . $row["slottime"] . " 
    Status: ". $row["slotstatus"] . " Count " . $count;


                                $count = 0; 
                            }
                            else 
                            {
                            echo "<br> False for " . $row["slottime"] . " 
    Status: ". $row["slotstatus"] . " Count " . $count; 



                            }
                    }   
                    else
                    {
                        echo "<br> False for " . $row["slottime"] .  " 
    Status: ". $row["slotstatus"] . " Count " . $count;
                        $count = 0;

                    }       
            }

    }
   else 
    {
        echo "Error: No results Found";
    }
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

应该在页面上看起来像这样

表格作为图像 ID slottime slotstatus 1 10:15 0 2 10:30 1 3 10:45 0 4 11:00 0 5 11:15 0 6 11:30 0 7 11:45 1

因此,如果用户想要30分钟的时段,则应根据上表显示选项

10:45 11:00 11:15

您可以使用这样的查询。 首先计算空闲插槽,然后您可以在外部SELECT中查询请求的插槽:

SELECT * from (
SELECT ts.*
            , @sclotcount := IF( ts.`slotstate` = 1, 0, @sclotcount +1 ) as freeslots
    FROM timeslots ts
    CROSS JOIN ( SELECT @sclotcount := 0) as init
    ORDER by id DESC
) as result
WHERE freeslots >= 3 -- request slots
ORDER by id;

样品

MariaDB [test]> SELECT * from ( SELECT ts.*, @sclotcount := IF( ts.`slotstate` = 1, 0, @sclotcount +1 ) as freeslots FROM timeslots ts CROSS JOIN ( SELECT @sclotcount := 0) as init ORDER by id DESC ) as result WHERE freeslots >= 3  ORDER by id;
+----+----------+-----------+-----------+
| id | slottime | slotstate | freeslots |
+----+----------+-----------+-----------+
|  1 | 00:00:00 |         0 |         3 |
| 10 | 02:15:00 |         0 |         3 |
| 14 | 03:15:00 |         0 |         7 |
| 15 | 03:30:00 |         0 |         6 |
| 16 | 03:45:00 |         0 |         5 |
| 17 | 04:00:00 |         0 |         4 |
| 18 | 04:15:00 |         0 |         3 |
| 22 | 05:15:00 |         0 |        75 |
| 23 | 05:30:00 |         0 |        74 |
| 24 | 05:45:00 |         0 |        73 |
...
| 83 | 20:30:00 |         0 |        14 |
| 84 | 20:45:00 |         0 |        13 |
| 85 | 21:00:00 |         0 |        12 |
| 86 | 21:15:00 |         0 |        11 |
| 87 | 21:30:00 |         0 |        10 |
| 88 | 21:45:00 |         0 |         9 |
| 89 | 22:00:00 |         0 |         8 |
| 90 | 22:15:00 |         0 |         7 |
| 91 | 22:30:00 |         0 |         6 |
| 92 | 22:45:00 |         0 |         5 |
| 93 | 23:00:00 |         0 |         4 |
| 94 | 23:15:00 |         0 |         3 |
+----+----------+-----------+-----------+
80 rows in set (0.001 sec)

MariaDB [test]> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM