简体   繁体   English

数组索引数组 php 上的 Foreach 循环

[英]Foreach loop on array of array indexes php

On the first page there is a form with a jquery script to add days.在第一页有一个带有 jquery 脚本的表单来添加天数。 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.我试图查看我在下一页 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.我认为这是因为我的 foreach 循环将考虑数组索引而不是数组中的每个事件。

book_avail.php 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 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"
  }
}

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.您可以做的是在禁用它们之前将输入的值复制到隐藏输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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