制作课堂预订系统PHP

时间:2011-08-07 21:52:46

标签: php database validation time comparison

我的php脚本需要帮助。我正在编写一个脚本,允许我的用户为他们的课程选择一个时间。时间列表锁定在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 |
+---------------------------------------------------------------------------------------+

1 个答案:

答案 0 :(得分:1)

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

//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;
}