php - 制作类(class)预订系统PHP

标签 php database validation time comparison

我的 php 脚本需要帮助。我正在编写一个脚本,允许我的用户为他们的类(class)选择时间。时间表锁定在 24 小时网格上,这意味着每小时只能上一节课。

此脚本的目标是向用户显示一个包含可用时间的简单表单。

一天有 24 次可能的时间一个房间。 *我有三个房间。*

后来我明白了如何制作表格,我不明白的是如果有三个房间如何显示可用时间。

这是我到目前为止得到的:

<?php
include 'db-connect.php';
if (isset($_GET['month']) && isset($_GET['day']) && isset($_GET['year'])) {

    $month = $_GET['month'];
    $day = $_GET['day'];
    $year = $_GET['year'];

    //string together date
    $date = $month."/".$day."/".$year;

    //return classes on this date
    $sql = mysql_query("SELECT start_time, server FROM classes WHERE date = '$date'");

    if (mysql_num_rows($sql)<=0) {

        echo "show all the times.";

    }else {

        $timelist = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24);
        $servers = array(1,2,3);

        while($query = mysql_fetch_array($sql)) {

                unset($timelist[$query['start_time'] - 1]);

        }
    }
}
?>

数据库示例:

+---------------------------------------------------------------------------------------+
| classID | trainerID | type | date      | start_time | duration | server | define      |
+---------------------------------------------------------------------------------------+
| 1       | 1         | 12   | 08/7/2011 | 9          | 60       | 1      | dummy class |
+---------------------------------------------------------------------------------------+

最佳答案

您可以只存储每次找到的数组数量,而不是您当前的策略,例如:

//define time list
$timelist = array_fill(1, 24, 0);

while($query1 = mysql_fetch_array($sql1)) {

    $timelist[$query1['start_time']]++;

}

while($query2 = mysql_fetch_array($sql2)) {

    $timelist[$query2['start_time']]++;

}

while($query3 = mysql_fetch_array($sql3)) {

    $timelist[$query3['start_time']]++;

}

$timelist = array_keys(array_filter($timelist, 'equals3'));

function equals3($x){
    return $x == 3;
}

现在 $timelist 是在所有三个查询中找到的时间数组。如果您想要一组从至少一个查询中丢失的时间,请使用它而不是最后几行:

$timelist = array_keys(array_filter($timelist, 'lessThan3'));

function lessThan3($x){
    return $x < 3;
}

关于php - 制作类(class)预订系统PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6976038/

相关文章:

database - Redis 的 Lua 脚本,它对键的值求和

mysql - 如何将行转换为没有具有唯一值的列的列?

php - 如何用php检查一个url是否是图片url?

javascript - 如何仅在字段为 $dirty 时应用 ng-pattern

php - 使用 PHP 可读的哈希导航时,如何向 url 添加参数?

php - isset() 不允许作为 array_filter() 的回调

php - SQL 不接受其中包含冒号 (":") 的 INSERT INTO

php - MySQL PDO 查询的奇怪行为

database - 同时搜索2个字段

jquery - 使用 javascript 验证人体高度