PHP/Mysql组合框值,不是Mysql中的whats,而是whats Not(Time Slots)


PHP / Mysql Combo box values, Not whats in mysql but whats not (Time Slots)

好的,我正在用PHP/Mysql制作一个预订系统,到目前为止,我只制作了一个测试插入数据的基本表单,但我想改进它,只显示可用的时隙。

在早些时候询问之后,到目前为止我已经使用了以下内容;

function mysql_fetch_rowsarr($result, $numass=MYSQL_BOTH) {
  $i=0;
  $keys=array_keys(mysql_fetch_array($result, $numass));
  mysql_data_seek($result, 0);
    while ($row = mysql_fetch_array($result, $numass)) {
      foreach ($keys as $speckey) {
        $got[$i][$speckey]=$row[$speckey];
      }
    $i++;
    }
  return $got;
}

$conn = mysql_connect($host,$user,$pass);
if (!$conn) {
    die('Could not connect: ' . mysql_error());
};
//Get rodays date
date_default_timezone_set('GMT');
$check_time = date("Y-m-d"); 
mysql_select_db($db, $conn);
mysql_set_charset('utf8',$conn);
$booking_check = mysql_query("SELECT * FROM Booking WHERE Booking_Start LIKE '%$check_time%'");
//if no bookings just echo the entire form but if there is do the else
if (mysql_num_rows($booking_check) == 0) {
    echo '
        <select name="time" id="time">
            <option value="0" selected>(Please Select:)</option>
            <option value="00:00">00:00 - 00:55</option>
            <option value="01:00">01:00 - 01:55</option>
            <option value="02:00">02:00 - 02:55</option>
            <option value="03:00">03:00 - 03:55</option>
            <option value="04:00">04:00 - 04:55</option>
            <option value="05:00">05:00 - 05:55</option>
            <option value="06:00">06:00 - 06:55</option>
            <option value="07:00">07:00 - 07:55</option>
            <option value="08:00">08:00 - 08:55</option>
            <option value="09:00">09:00 - 09:55</option>
            <option value="10:00">10:00 - 10:55</option>
            <option value="11:00">11:00 - 11:55</option>
            <option value="12:00">12:00 - 12:55</option>
            <option value="13:00">13:00 - 13:55</option>
            <option value="14:00">14:00 - 14:55</option>
            <option value="15:00">15:00 - 15:55</option>
            <option value="16:00">16:00 - 16:55</option>
            <option value="17:00">17:00 - 17:55</option>
            <option value="18:00">18:00 - 18:55</option>
            <option value="19:00">19:00 - 19:55</option>
            <option value="20:00">20:00 - 20:55</option>
            <option value="21:00">21:00 - 21:55</option>
            <option value="22:00">22:00 - 22:55</option>
            <option value="23:00">23:00 - 23:55</option>
        </select>
    ';
} else {

    //counter set to 23 because of 0
    $counter = 23;
    $i = 0;
    $booked_times = mysql_fetch_rowsarr($booking_check);
    $num_booked = mysql_num_rows($booking_check);
    echo date("H", strtotime($booked_times[0]['Booking_Start']));
    echo date("H", strtotime($booked_times[1]['Booking_Start']));
    echo "<select name='time' id='time'>";
    echo "<option value='0' selected>(Please Select:)</option>"; 
    while($i<=$counter) {
        //$H is used to compare if the hour slot is booked this adds a 0 to the front of the varible if under 10 for the 24H time format.
        if ($i < 10){
            $H = "0".$i;
        } else {
            $H = $i;
        };
        //Loop through the times that are booked and see if it is the same as the $H which is the current count of the loop so it should set skip to 1 if the time is booked.
        for ($x = 0; $x < $num_booked; $x++){
            if(date("H", strtotime($booked_times[$x-1]['Booking_Start'])) == $H){
                $skip = 1;
            }else{
                $skip = 0;
            };
        };
        if($skip != 1){
            if ($i < 10){
                echo '<option value="0'.$i.':00">0'.$i.':00 - 0'.$i.':55</option>';
                $i++;
            }else{
                echo '<option value='.$i.':00">'.$i.':00 - '.$i.':55</option>';
                $i++;
            };
        } else {
            $i++;
        };
    };
    echo "</select>";
};

但我认为它很原始,但我仍在努力。我想我已经到了那里,我需要在现在之前切断所有的时间。

考虑从所有可能的时隙列表开始,无论是否可用。现在使用上面的内容读取所有占用的插槽,然后从完整列表中删除

我使用了这样的东西:

<?php
function mysql_fetch_rowsarr($result, $numass=MYSQL_BOTH) {
$i=0;
$keys=array_keys(mysql_fetch_array($result, $numass));
mysql_data_seek($result, 0);
while ($row = mysql_fetch_array($result, $numass)) {
  foreach ($keys as $speckey) {
    $got[$i][$speckey]=$row[$speckey];
  }
$i++;
}
 return $got;
}
function convertDate($date){
$date_year=substr($date,1,4);
$date_month=substr($date,6,7);
$date_day=substr($date,8,10);
$date=date("Y-m-d", mktime(0,0,0,$date_month,$date_day,$date_year));
return $date; 
};
$is_ajax = $_REQUEST['is_ajax'];
if (!isset($_POST['date'])) {
echo "<option value='0' selected>Select Date...</option>";
}else{
include 'config.php';
$conn = mysql_connect($host,$user,$pass);
if (!$conn) {
    die('Could not connect: ' . mysql_error());
};
//Get rodays date
date_default_timezone_set('Europe/London');
$date = $_POST['date'];
$check_date = convertDate($date);
$today = date("Y-m-d");
$the_hour = date("H");      
mysql_select_db($db, $conn);
mysql_set_charset('utf8',$conn);
$booking_check = mysql_query("SELECT * FROM Booking WHERE Booking_Start LIKE '%$check_date%'");
//if no bookings just echo the entire form but if there is do the else
if (mysql_num_rows($booking_check) == 0) {
    //counter set to 23 because of 0
    $counter = 23;
    $i = 0;
    if($check_date == $today && $the_hour == 23){
        echo "<option value='0' selected>No Slots. Sorry.</option>";
    }else{
        while($i<=$counter) {
            if($check_date == $today){
                if ($i > $the_hour){
                    if ($i < 10){
                        echo '<option value="0'.$i.':00">0'.$i.':00 - 0'.$i.':55</option>';
                        $i++;
                    }else{
                        echo '<option value="'.$i.':00">'.$i.':00 - '.$i.':55</option>';
                        $i++;
                    };
                }else{
                    $i++;
                };
            }else{
                if ($i < 10){
                    echo '<option value="0'.$i.':00">0'.$i.':00 - 0'.$i.':55</option>';
                    $i++;
                }else{
                    echo '<option value="'.$i.':00">'.$i.':00 - '.$i.':55</option>';
                    $i++;
                };
            };
        };
    };
} else {
    //counter set to 23 because of 0
    $counter = 23;
    $i = 0;
    $avalible_slots = 23;
    $booked_times = mysql_fetch_rowsarr($booking_check);
    $num_booked = mysql_num_rows($booking_check);
    $H = 0;
    if($check_date == $today && $the_hour == 23){
        echo "<option value='0' selected>No Slots. Sorry.</option>";
    }else{
        while($i<=$counter) {
            //$H is used to compare if the hour slot is booked this adds a 0 to the front of the varible if under 10 for the 24H time format.
            if ($i < 10){
                $H = "0".$i;
            } else {
                $H = $i;
            };
            //Loop through the times that are booked and see if it is the same as the $H which is the current count of the loop so it should set skip to 1 if the time is booked.
            $x = 0;
            while($x < $num_booked){
                if(date("H", strtotime($booked_times[$x]['Booking_Start'])) == $H){
                    $skip = 1;
                    break;
                }else{
                    $skip = 0;
                    $x++;
                };
            };
            if($check_date == $today){
                if($skip != 1 && $i > $the_hour){
                    if ($i < 10){
                        echo '<option value="0'.$i.':00">0'.$i.':00 - 0'.$i.':55</option>';
                        $i++;
                    }else{
                        echo '<option value="'.$i.':00">'.$i.':00 - '.$i.':55</option>';
                        $i++;
                    };
                }else{
                    $i++;
                    $avalible_slots--;
                };
            }else{
                if($skip != 1){
                    if ($i < 10){
                        echo '<option value="0'.$i.':00">0'.$i.':00 - 0'.$i.':55</option>';
                        $i++;
                    }else{
                        echo '<option value="'.$i.':00">'.$i.':00 - '.$i.':55</option>';
                        $i++;
                    };
                }else{
                    $i++;
                    $avalible_slots--;
                };
            };
        };
        echo $avalible_slots;
        echo $num_booked;
    };
};
};
?>