【问题标题】:Time Slot Booking Script - Check availability时间段预订脚本 - 检查可用性
【发布时间】:2019-03-27 20:25:33
【问题描述】:

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

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

我将一天分成 15 分钟时段,然后创建了一个 $slotstatus,1 表示忙碌,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";
    }
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

在页面上应该是这样的

table as image 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

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    您可以使用这样的查询。它首先计算空闲插槽和你 可以在外层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]> 
    

    【讨论】:

    • 只是想用我非常有限的 MYSQL 知识来完成这项工作,交叉连接,这是否意味着我有一个单独的表,称为 ts,就像上面的带有 freeslots 的表一样?还是我傻?
    • FROM timeslots ts仅表示ts是表名timeslots的别名。它只是一个较短的符号。所以你可以写 ts.fieldname 而不是 timeslots.fieldname 。 CROSS JOIN 只是一个帮助器(在这种情况下)初始化用户变量 ** sclotcount** .. 如果你不初始化你不能像 @sclotcount +1
    • 你可以直接在你的服务器上运行这个查询
    • 感谢您像梦一样工作,我不知道为什么,但我什至没有想到要对 MYSQL 查询进行所有计算。
    猜你喜欢
    • 1970-01-01
    • 2021-03-18
    • 1970-01-01
    • 2020-09-21
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多