根据MySQL Query禁用页面加载时的选择选项

时间:2017-01-17 19:55:16

标签: php jquery mysql

与标题一样,我有一个选择选项输入,我需要根据它们是否已经两次添加到表中来禁用某些选项,如果它们已经两次添加到表中,则禁用特定时间选项选择下拉列表。

在下面的示例代码中,在表格中您可以看到在下午12:15时已经有两个条目用于2017年1月18日。因此,当我加载页面时,我需要在选项列表中禁用12:15 PM时选择日期为1/18/2017。因此,如果我加载页面并选择1/18/2017作为日期,则将禁用12:15 PM的选项。

我觉得我应该能够使用一个单独的php文件来执行此操作,该文件将使用QUERY和COUNT从表中获取数据,但我不确定如何攻击它。

示例表:

TIMESTAMP           Datepicker  Timepicker
2017-01-17 08:44:15 01/21/2017  12:00 PM
2017-01-17 09:52:00 01/20/2017  11:30 AM
2017-01-17 10:07:09 01/18/2017  12:15 PM
2017-01-17 10:08:58 01/18/2017  12:15 PM

示例选择选项:

<input class="dateselect" id="datepicker" name="datepicker" type="text" placeholder="Select Date">
                            <select class="timeselect" id="timepicker" name="timepicker">
                            <option value="10-15 Minutes">Select Time</option>
                            <option value="08:00 AM">08:00 AM</option>
                            <option value="08:15 AM">08:15 AM</option>
                            <option value="08:30 AM">08:30 AM</option>
                            <option value="08:45 AM">08:45 AM</option>
                            <option value="09:00 AM">09:00 AM</option>
                            <option value="09:15 AM">09:15 AM</option>
                            <option value="09:30 AM">09:30 AM</option>
                            <option value="09:45 AM">09:45 AM</option>
                            <option value="10:00 AM">10:00 AM</option>
                            <option value="10:15 AM">10:15 AM</option>
                            <option value="10:30 AM">10:30 AM</option>
                            <option value="10:45 AM">10:45 AM</option>
                            <option value="11:00 AM">11:00 AM</option>
                            <option value="11:15 AM">11:15 AM</option>
                            <option value="11:30 AM">11:30 AM</option>
                            <option value="11:45 AM">11:45 AM</option>
                            <option value="12:00 PM">12:00 PM</option>
                            <option value="12:15 PM">12:15 PM</option>
                            <option value="12:30 PM">12:30 PM</option>
                            <option value="12:45 PM">12:45 PM</option>
</select>

任何帮助都将非常感谢我在网上找到的所有东西并不是我想要的。

PHP暂定文件:

$host_name  = "xxxxxxx";
$database   = "xxxxxx";
$user_name  = "xxxxx";
$password   = "xxxxx";


$connect = mysqli_connect($host_name, $user_name, $password, $database);

$sql = "SELECT Datepicker, 
   Timepicker

FROM shuttlerequests
   INNER JOIN (SELECT Datepicker
           FROM   shuttlerequests
           GROUP  BY Timepicker
           HAVING COUNT(id) > 1) dup
       ON shuttlerequests.Datepicker = dup.Timepicker;";

if ($connect->query($sql) === TRUE) {
return $sql;
} else {
echo "Error: " . $sql . "<br>" . $connect->error;
}

1 个答案:

答案 0 :(得分:0)

我认为你走在正确的轨道上。我会创建一个PHP文件,返回一个&#34;不可用的时间列表&#34;基于输入日期参数​​。然后,根据我回来的列表,适当地禁用/启用选项。试试这个样本:

<强>的Javascript

function getTimes() {
    var date = $('#datepicker').val();

  //REPLACE THIS WITH A CALL TO YOUR PHP FILE WHICH SHOULD RETURN THE DISABLED TIMES FOR A GIVEN DATE.
  $.ajax({
    url: 'getdisabledtimes.php',
    data: { date: date },
    success: handleTimes
  });
}

function handleTimes(obj) {
  //INITIALLY SET ALL OPTIONS TO ENABLED
  $('#timepicker option').removeAttr('disabled');
  //DISABLE THE OPTIONS THAT WERE RETURNED FROM THE FUNCTION
  for (var i in obj) {
      $('#timepicker option[value="' + obj[i] + '"]').attr('disabled','disabled');
  }
}

jsFiddle:https://jsfiddle.net/mspinks/3y69qygu/4/

- 点击&#34;获取时间&#34;按钮。 &#34;获取时间&#34;此示例中的按钮模拟在代码中选择日期。这取决于您的实施(点击,加密,模糊等)。

可能的PHP(尚未经过测试)

$host_name  = "xxxxxxx";
$database   = "xxxxxx";
$user_name  = "xxxxx";
$password   = "xxxxx";


$connect = mysqli_connect($host_name, $user_name, $password, $database);

$sql = "SELECT Datepicker, 
   Timepicker

FROM shuttlerequests
   INNER JOIN (SELECT Datepicker
           FROM   shuttlerequests
           GROUP  BY Timepicker
           HAVING COUNT(id) > 1) dup
       ON shuttlerequests.Datepicker = dup.Timepicker;";


$result = $connect ->query($sql);

if ($result->num_rows > 0) {
    //CREATE AN ARRAY FOR STORAGE OF EACH TIME ENTRY (TIMEPICKER)
    $disabledTimes = array();
    //ITERATE THROUGH EACH DATA ROW THAT WAS RETURNED
    while( $row = mysql_fetch_assoc( $result)) {
        //ADD THE 'TIMEPICKER' ENTRY TO THE ARRAY
        //NOTE: YOU MAY NEED TO FORMAT THE TIME STRING TO MATCH THE SELECT LIST. SHOULD BE IN THIS FORMAT: "08:00 AM"
        array_push($disabledTimes , $row["Timepicker"]);
    }
    //SERIALIZE THE ARRAY INTO A JSON STRING. THIS SHOULD OUTPUT SOMETHING LIKE "['08:00 AM','12:30 PM']"
    echo(json_encode($myArray));
} else {
    echo "[]"; //NO RESULTS HERE
}