尝试建立一个基本的预订系统,其中用户可以说他们需要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
答案 0 :(得分:0)
您可以使用这样的查询。它首先计算空闲插槽,然后您 可以在外部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]>