简体   繁体   中英

Making Class Booking system PHP

I need help with my php script. I am working on a script that allows my users to choose a time for their class. The time list is locked on a 24 hour grid, meaning a class can only happen every hour.

The goal of this script is to show the user a simple form with the available times.

There are 24 possible times in a day for one room. * I have three rooms. *

I understand how to make the form later on, what I don't get is how to show available times if there are three rooms.

Here is what I got so far:

<?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]);

        }
    }
}
?>

Database Example:

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

Instead of your current strategy you could just store the number of arrays each time is found in, like:

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

Now $timelist is an array of times that were found in all three queries. If you want an array of times that were missing from at least one query use this instead of the last few lines:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM