PHP获取 - 不返回所有行进行数组检查

时间:2017-10-24 20:05:09

标签: php mysql

我正在按小时预订系统工作。基于选定的DAY,我有一个JQuery函数设置来更改表单上的TIME选择。

作为测试,我只是想列出可用的时间并采取。

我得到的问题是我希望根据所选的DAY显示为(21:00和22:00)的TIME值无法正常工作。

21:00正在接受采取。 22:00没有被正确接收。

我的表格包含以下设置和测试数据:

ID | NAME    | DAY | TIME  | HOTEL | HOST
1  | Event 1 | 2   | 21:00 | South | Joe
2  | Event 2 | 2   | 22:00 | South | Matt

在我的活动预订页面上,我有以下内容:

脚本改变时间选择:

<script>
  function getData()
  {
      var formData = new FormData($("#eventbook")[0]);
      $.ajax({
          url: 'eventdata.php',
          type:'POST',
          data: formData,
          processData: false,
          contentType: false,
          cache: false,
          mimeType: 'multipart/form-data',
          success: function(html)
          {
              $("#eventitems").html(html);
          }
      });
      return false
  }
</script>

FORM:

<form id="eventbook" action="">
<div class="form-group">
  <p class="text-center">
    <label for="selOption">Event Day:</label>
    <select class="form-control" id="selOption" onchange="getData()" name="selOption">
      <option value="1">Monday</option>
      <option value="2">Tuesday</option>
      <option value="3">Wednesday</option>
      <option value="4">Thursday</option>
      <option value="5">Friday</option>
      <option value="6">Saturday</option>
      <option value="7">Sunday</option>
    </select>
  </p>
</div>
<div id="eventitems">
  <?php
    $times = array(
        "00:00" => "00:00",
        "01:00" => "01:00",
        "02:00" => "02:00",
        "03:00" => "03:00",
        "04:00" => "04:00",
        "05:00" => "05:00",
        "06:00" => "06:00",
        "07:00" => "07:00",
        "08:00" => "08:00",
        "09:00" => "09:00",
        "10:00" => "10:00",
        "11:00" => "11:00",
        "12:00" => "12:00",
        "13:00" => "13:00",
        "14:00" => "14:00",
        "15:00" => "15:00",
        "16:00" => "16:00",
        "17:00" => "17:00",
        "18:00" => "18:00",
        "19:00" => "19:00",
        "20:00" => "20:00",
        "21:00" => "21:00",
        "22:00" => "22:00",
        "23:00" => "23:00",
    );
    if (!isset($_REQUEST['selOption'])) {
        $day = 1;
    }
    $dayInfo = $dbh->prepare("SELECT * FROM events WHERE day=:day");
    $dayInfo->execute(array(":day"=>$day));
    $dayInfoResult = $dayInfo->fetch(PDO::FETCH_ASSOC);
  ?>
  <div class="form-group">
    <p class="text-center">
        <?php
          foreach ($times as $key => $value) {
              if (in_array($value, $dayInfoResult)) {
                  echo "{$key} with value of {$value} is taken.<br>";
              } else {
                  echo "{$key} with value of {$value} is available.<br>";
              }
          }
        ?>
    </p>
  </div>
</div>
</form>

在eventdata.php中我有以下内容:

<?php
require_once 'includes/config.php';
$times = array(
    "00:00" => "00:00",
    "01:00" => "01:00",
    "02:00" => "02:00",
    "03:00" => "03:00",
    "04:00" => "04:00",
    "05:00" => "05:00",
    "06:00" => "06:00",
    "07:00" => "07:00",
    "08:00" => "08:00",
    "09:00" => "09:00",
    "10:00" => "10:00",
    "11:00" => "11:00",
    "12:00" => "12:00",
    "13:00" => "13:00",
    "14:00" => "14:00",
    "15:00" => "15:00",
    "16:00" => "16:00",
    "17:00" => "17:00",
    "18:00" => "18:00",
    "19:00" => "19:00",
    "20:00" => "20:00",
    "21:00" => "21:00",
    "22:00" => "22:00",
    "23:00" => "23:00",
);
$day = $_REQUEST['selOption'];
$dayInfo = $dbh->prepare("SELECT * FROM events WHERE day=:day");
$dayInfo->execute(array(":day"=>$day));
$dayInfoResult = $dayInfo->fetch(PDO::FETCH_ASSOC);
?>
<div class="form-group">
  <p class="text-center">
      <?php
        foreach ($times as $key => $value) {
            if (in_array($value, $dayInfoResult)) {
                echo "{$key} with value of {$value} is taken.<br>";
            } else {
                echo "{$key} with value of {$value} is available.<br>";
            }
        }
      ?>
  </p>
</div>

选择星期一,星期三,星期四,星期五,星期六或星期日后,所有TIMES都会正确显示为“值为VALUE的KEY。”。

选择星期二时,它只显示21:00,但不是22:00。

1 个答案:

答案 0 :(得分:1)

你需要使用fetchall而不是fetch。

替换

   decode16 b. _1
encode16
   encode16 b. _1
16&#.@('0123456789ABCDEF'&i.) :.({&'0123456789ABCDEF'@(16 16 16 16 16 16&#:))

使用

$dayInfoResult = $dayInfo->fetch(PDO::FETCH_ASSOC);

fetch :从结果集中获取下一行。

fetchAll :返回包含所有结果集行的数组

还需要修改代码以检查花费的时间。

$dayInfoResult = $dayInfo->fetchAll(PDO::FETCH_ASSOC);

在你的HTML代码中

// fetch all data from database for that day
$dayInfoResult = $dayInfo->fetchAll(PDO::FETCH_ASSOC);
// define an array
$taken_times = array();
// loop the database results
foreach($dayInfoResult as $key=>$value){
    // new array to get all the taken times for that day
    $taken_times[] = $value['TIME'];
}