Tiernan91
Tiernan91

Reputation: 41

Foreach loop on array of array indexes php

On the first page there is a form with a jquery script to add days. If I just use one day I cant get all the info I need. But when I add multiple days I just get the last entry.

I am trying to see every day of the event I book in the table on the next page book_gear.php. But so far I am only see one day of the event .

I think it is because my foreach loop is going thought the array indexs rather than each event in the array.

book_avail.php

<?php require_once('../Include/initialize.php'); ?>
<!doctype html>
<html>
 <!--Sets page title in browser-->
<?php $page_title = 'Schedule'; ?>
  <!-- Imports header file with necessary scripts -->
<?php include ('../Include/header.php'); ?>
<!-- Imports Top navigation bar -->
<?php include ('../Include/navibar.php'); ?>
<!-- Imports javascript file to allow arrange by column -->
<?php include_once ('../Include/sort_by.php'); ?>
<?php include_once ('../Include/mysql_queries.php');
//loads all classes
include_once ('../Include/class-autoload.inc.php'); ?>
<script>
    $(document).ready(function() {
  var i = 1;
  $("#add_row").click(function() {
  $('tr').find('input').prop('disabled',true)
    $('#addr' + i).html("<td>" + (i + 1) +
        "</td><td><input type='text' value='Day" + (i + 1) +"' name='event[" + i + "][eventNameInitialQuery]' class='form-control input-md'/></td>\n\
            <td><input type='date' name='event[" + i + "][dateFromIntialQuery]' placeholder='date' class='form-control' /></td>\n\
            <td><input type='time' name='event[" + i + "][timeFromIntialQuery]' placeholder='time' class='form-control' /></td>\n\
            <td><input type='date' name='event[" + i + "][dateToIntialQuery]' placeholder='date' class='form-control' />\n\</td>\n\
            <td><input type='time' name='event[" + i + "][timeToIntialQuery]' placeholder='time' class='form-control' />\n\</td>");

    $('#tab_logic').append('<tr id="addr' + (i + 1) + '"></tr>');
    i++;
  });
});
</script>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>


<?php
//sets user location or builing
if (isset($_SESSION['location'])) {
$userLocation = $_SESSION['location']?? '';}
$build_set = build_dropdown($userLocation);
?>

 <!--Main Id is the bulk of the page-->
<div id="main">
 <!--Sets side navigation bar-->
<div class="sidenav">
    <div>
     <!--Includes side navigation script -->
    <?php include_once ('../Include/side_navi_login.php'); ?>
    </div>
</div>
    <br><br>
<body>

     <!--Sets Title "Schedule Event"-->

    <h1 class="book_event_title">Schedule Event</h1>
     <!--This table is the form that runs along the top of the page-->
<div class="container">
  <div class="row clearfix">
    <div class="col-md-12 column">
        <form  action="book_gear.php?" method="post">
      <table class="table table-bordered table-hover" id="tab_logic">
        <thead>
          <tr>
            <th class="text-center">
              Day
            </th>
            <th class="text-center">
              Event Name
            </th>
            <th class="text-center">
              Date From
            </th>
            <th class="text-center">
              Time From
            </th>
             <th class="text-center">
              Date To
            </th>
            <th class="text-center">
              Time To
            </th>
            <th class="text-center">
              Building
            </th>
            <th class="text-center">
              VP Present
            </th>
            <th class="text-center">
              Ticket
            </th>
          </tr>
        </thead>
        <tbody>
          <tr id='addr0'>
            <td>
              1
            </td>
            <td>
              <input type="text" name='event[0][eventNameInitialQuery]' placeholder='Event Name' class="form-control" />
            </td>
            <td>
              <input type="date" name='event[0][dateFromIntialQuery]' placeholder='date' class="form-control" />
            </td>
             <td>
              <input type="time" name='event[0][timeFromIntialQuery]' placeholder='time' class="form-control" />
            </td>
            <td>
              <input type="date" name='event[0][dateToIntialQuery]' placeholder='date' class="form-control" />
            </td>
             <td>
              <input type="time" name='event[0][timeToIntialQuery]' placeholder='time' class="form-control" />
            </td>
            <!-- Gets the dropdown menu from a method in the site view class -->
            <td>
            <select name="buildingCodeIntialQuery" id="site_value" required>
                <option value="" disabled selected>-Building-</option>
                <?php
                    $buildList = new SiteView;
                    $buildList->showMyBuildings($userLocation) ;
                    ?>
            </select>
            </td>
            <td>
               <input  type="hidden" name="vpInitialQuery" value="No" class="form-control"/>
        <input type="checkbox" name="vpInitialQuery" value="Yes" class="form-control"/>
            </td>
            <td>
              <input type="text" name='ticketInitialQuery' placeholder='Link to Event Ticket' class="form-control" />
            </td>
          </tr>
          <tr id='addr1'></tr>
        </tbody>
      </table>
    <div align="right" id="schedule_button">
        <input type="submit" value="Schedule Event"/>
    </div>
            </form>
    </div>
  </div>
  <button id="add_row" class="btn btn-primary btn-lg pull-left">Add Day</button>
</div>
</div>
</body>
 <!--Includes bottom footer-->
<?php include('../Include/footer.php'); ?>
</html>

book_gear.php

<?php require_once('../Include/initialize.php'); ?>
<!doctype html>
<html>
 <!--Sets page title in browser-->
<?php $page_title = 'Schedule'; ?>
  <!-- Imports header file with necessary scripts -->
<?php include ('../Include/header.php'); ?>
<!-- Imports Top navigation bar -->

<!-- Imports javascript file to allow arrange by column -->
<?php include_once ('../Include/sort_by.php');
include_once ('../Include/mysql_queries.php'); ?>

<?php
//once the person is logged in their site is stored in this variable
if(isset($_SESSION['logins'])) {
            $users_site = $_SESSION['location'];
            }

if(isset($_POST['event'])) {
            $events = $_POST['event'];
            }

foreach($events as $unique =>$event){
  ?>
  <table>
    <tr>
    <th>event name</th>
    <th>datefrom</th>
    <th>timefrom</th>
    <th>dateto</th>
    <th>timeto</th>
  </tr>

  <td><?php echo $event['eventNameInitialQuery'];?></td>
  <td><?php echo $event['dateFromIntialQuery'];?></td>
  <td><?php echo $event['timeFromIntialQuery'];?></td>
  <td><?php echo $event['dateToIntialQuery'];?></td>
  <td><?php echo $event['timeToIntialQuery'];?></td>
</table><?php ;}?>

EDIT

After suggested in the comments ran

echo '<pre>'; var_dump($events);echo '</pre>';die(); before the foreach loop

Result

array(1) {
  [1]=>
  array(5) {
    ["eventNameInitialQuery"]=>
    string(4) "Day2"
    ["dateFromIntialQuery"]=>
    string(10) "2020-07-23"
    ["timeFromIntialQuery"]=>
    string(5) "18:57"
    ["dateToIntialQuery"]=>
    string(10) "2020-07-11"
    ["timeToIntialQuery"]=>
    string(5) "19:54"
  }
}

Upvotes: 0

Views: 75

Answers (1)

Terminator-Barbapapa
Terminator-Barbapapa

Reputation: 3126

I've done a quick test and the problem is with disabling the earlier inputs when adding a new event row. What you could do is copy the values of your inputs to hidden inputs before you disable them.

Upvotes: 1

Related Questions