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>";
         }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在ns3中实现路径的自由切换
  • ¥20 SpringBoot+Vue3
  • ¥15 IT从业者的调查问卷
  • ¥65 LineageOs-21.0系统编译问题
  • ¥30 关于#c++#的问题,请各位专家解答!
  • ¥15 App的会员连续扣费
  • ¥15 不同数据类型的特征融合应该怎么做
  • ¥15 用proteus软件设计一个基于8086微处理器的简易温度计
  • ¥15 用联想小新14Pro
  • ¥15 multisim中关于74ls192n和DSWPK开关仿真图分析(减法计数器)