dsizd368332 2017-10-24 20:05
浏览 19
已采纳

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

I am working on an hourly booking system. Based on the selected DAY, I have a JQuery function setup to change the TIME selection on my form.

As a test, I am just trying to list the TIMES available and taken.

The problem I am getting is that the TIME values I want to show as taken (21:00 and 22:00) based on the DAY selected, is not working correctly.

21:00 is correctly picking up as being taken. 22:00 is not being correctly picked up as being taken.

My table has the following setup and test data:

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

On my event booking page, I have the following:

SCRIPT TO CHANGE TIME SELECTION:

<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>

In eventdata.php I have the following:

<?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>

Upon selecting Monday, Wednesday, Thursday, Friday, Saturday, or Sunday, all the TIMES are correctly showing as "KEY with value of VALUE is available.".

Upon selecting Tuesday, it is showing only 21:00 as being taken, but not 22:00.

  • 写回答

1条回答 默认 最新

  • dongyi9298 2017-10-24 20:15
    关注

    You need to use fetchall instead of fetch.

    Replace

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

    With

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

    fetch : Fetches the next row from a result set.

    fetchAll : Returns an array containing all of the result set rows

    And also need to modify the code to check the taken time.

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

    Inside your html code

    foreach ($times as $key => $value) {
        // check the time in new array
        if (in_array($value, $taken_times)) {
             echo "{$key} with value of {$value} is taken.<br>";
          } else {
              echo "{$key} with value of {$value} is available.<br>";
         }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥15 pyqt信号槽连接写法
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。