简体   繁体   中英

PHP Fetch - Not Returning All Rows for Array Check

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.

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>";
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM