2

I'm having a problem with a hard coded array I'm using to test. I'm looping some html and php to create a header list for each top level item, which works. But I'm looping again to create a modal for each item and it's only corresponding to the first item.

Here's the array, as it's hard-coded:

  <?php 

  $displays = array(
    "Company" => array(
      "Company One Main"=>array(
        "Displays"=>array(
          "Room One"=>array(
            "Page"=>array(
              "Type"=>"News",
              "Expiration"=>"06/12/2018"),
          ),
          "Room Two"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
          "Room Three"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
        ),
      ),
      "Company One Other"=>array(
        "Displays"=>array(
          "Room Two"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
          "Room Three"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
        ),
      ),
      "Company Two Main"=>array(
        "Displays"=>array(
          "Room One"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
          "Room Two"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
        ),
      ),
      "Company Two Other"=>array(
        "Displays"=>array(
          "Room One"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
          "Room Two"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
          "Room Three"=>array(
            "Page"=>array(
              "Type"=>"Social",
              "Expiration"=>"06/18/2018"),
          ),
        ),
      ),
    ),
  );

  ?>

So my first bit of code works correctly by creating a list of the 4 areas, like so:

The problem is, when I click edit for any of the four companies, it shows the modal containing only the info for the first element in the array, Company One Main. The layout is right but it only shows the rooms, page types and expirations for that first company rather than each list item having a modal for it's own respective info.

So no matter which one I click on I see this:

I may just be looping incorrectly. Here's the PHP and HTML

  /*These first two lines are correct, they created the list of areas with 'edit' links*/

  <?php foreach($displays["Company"] as $area_name => $area_details): ?>
    <h6><?php echo $area_name ?><a href="#" data-toggle="modal" data-target="#expiringDisplays">Edit</a></h6>
      <!-- Modal -->
      <div class="modal fade" id="expiringDisplays" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
          <div class="modal-content">
            <div class="modal-header">
            <?php foreach($area_details["Displays"] as $d=>$v): ?>
            <h5 class="modal-title" id="modalLabel"><?php echo  $area_name . " - " . $d?></h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
            <!-- END MODAL HEADER -->
            </div>
            <div class="modal-body">
              <?php foreach($v as $k=>$n): ?>
              <!--Body of the Modal for expired displays-->
              <br>
              <h6><?php echo $n['Type'] ?></h6>
              <h6>Date/Time Expiring: <?php echo $n["Expiration"] ?></h6>
              <h6>Set New Expiration:</h6>                                     
              <a href="#" style="float:left;">View Display</a>
              <br>
              <hr>
              <?php endforeach; ?>
              <?php endforeach; ?>
              <!-- END MODAL BODY -->
            </div>
            <div class="modal-footer">
              <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
              <button type="button" class="btn btn-primary">Save changes</button>
              <!-- END MODAL FOOTER -->
            </div>
            <!-- END MODAL CONTENT -->
          </div>
          <!-- END MODAL DIALOGUE -->
      </div>
      <!-- END MODAL -->
      </div>
  <?php endforeach; ?>
2
  • 1
    because in loop you have given same id to modal div and anchor tag Commented Jun 18, 2018 at 12:26
  • I was under the impression I was getting each ID, and then the inside loops would perform accordingly for each element. How should I alter this properly? Commented Jun 18, 2018 at 12:36

2 Answers 2

1

You are giving the same anchor link to all of your Edit links. By default it will select the 1st element having this id. You need to provide each modal its own id.

If you are sure the "company names" are unique you can use them as id (replacing some characters before), but you could use a numeric id suffix (which I think would be better):

<?php
$n_id = 0;
foreach($displays["Company"] as $area_name => $area_details) {
    ?>
    <h6><?php echo $area_name ?><a href="#" data-toggle="modal" data-target="#modal_<?php echo $n_id; ?>">Edit</a></h6>
    <!-- Modal -->
    <div class="modal fade" id="modal_<?php echo $n_id; ?>" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true">
    <!-- The rest of your code ... -->
    </div>
    <?php
    $n_id += 1;
}
?>
Sign up to request clarification or add additional context in comments.

1 Comment

@TomN. I'm sorry I forgot to write it ! Glad it helped you
0

try something like this

$i = 0 

foreach ($data as $k => $v) {
  echo '<a href="#" data-toggle="modal" data-target="#expiringDisplays_'.$i.'">Edit</a>';
// and your give the same to div tag like this
echo '<div class="modal fade" id="expiringDisplays_'.$i.'" tabindex="-1" role="dialog" aria-labelledby="modalLabel" aria-hidden="true"></div>';
$i++;
}

hope you are getting my point

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.