[英]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.