1

I have a button within a php foreach statement which loads a modal. How can i pass the particular id ($animalid) to the modal? I have it kind of working but the modal will load the same id in each modal popup. See my php foreach code below and also part of the modal.

$pdo2 = Database::connect();
               $sql2 = 'SELECT * FROM animals WHERE riderid = '.$data[id].' AND hp != "Choose One"';
               foreach ($pdo2->query($sql2) as $row) {
                        echo '<tr>';
                        echo '<td>'. $row['hp'] . '</td>';
                        echo '<td>'. $row['hpname'] . '</td>';
                        echo '<td>'. $row['hpage'] . '</td>';
                        echo '<td>'. $row['hpcolour'] . '</td>';
                        echo '<td>'. $row['hpmicro'] . '</td>';
                        echo '<td>';
                        echo '<button class="btn btn-default btn-xs" id="float-right" data-toggle="modal" data-target=".bs-example-modal-lg-2"><span class="glyphicon glyphicon-pencil"></span> Update</button>';
                        echo ' ';
                        echo '<a class="btn btn-default btn-xs" href="#"><span class="glyphicon glyphicon-trash"></span> Delete</a>';
                        echo '</td>';
                        echo '</tr>';
                        $animalid = $row['id'];
               }

               Database::disconnect();
              ?>

Modal Code

<div class="modal-body">




        <form name="editanimal" id="editanimal" class="form-horizontal" action="updateanimal.php" method="post">

        <span class="form-break">

        <?php 
        // Get Animal id
        //$animalid = $data['id'];
        echo $animalid;
        ?>

2 Answers 2

1

$animalid is in the loop. If you want to use that value, you can store the data to other array and use it. Hope you get some inspiration.

PHP:

<?php
$pdo2 = Database::connect();
$sql2 = 'SELECT * FROM animals WHERE riderid = '.$data[id].' AND hp != "Choose One"';

$list = array();

foreach ($pdo2->query($sql2) as $row) {

  $item = array();

  $content = '<tr>';
  $content .= '<td>'. $row['hp'] . '</td>';
  $content .= '<td>'. $row['hpname'] . '</td>';
  $content .= '<td>'. $row['hpage'] . '</td>';
  $content .= '<td>'. $row['hpcolour'] . '</td>';
  $content .= '<td>'. $row['hpmicro'] . '</td>';
  $content .= '<td>';
  $content .= '<button class="btn btn-default btn-xs" id="float-right" data-toggle="modal" data-target=".bs-example-modal-lg-2"><span class="glyphicon glyphicon-pencil"></span> Update</button>';
  $content .= ' ';
  $content .= '<a class="btn btn-default btn-xs" href="#"><span class="glyphicon glyphicon-trash"></span> Delete</a>';
  $content .= '</td>';
  $content .= '</tr>';

  $item['id'] = $row['id'];
  $item['content'] = $content;

  $list[] = $item;

}

Database::disconnect();
?>

HTML:

<div class="modal-body">
  <form name="editanimal" id="editanimal" class="form-horizontal" action="updateanimal.php" method="post">
    <span class="form-break"></span>
    <?php foreach($list as $item){ ?>
      <?php echo $item['id'];?>
    <?php } ?>
    or..
    <table>
      <?php foreach($list as $item){ ?>
          <?php echo $item['content'];?>
      <?php } ?>
    </table>
    <!-- ... -->
  </form>
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

I was looking for an explication like this since a while and did my job easily with that ^_^, thanks !
0

$animalid is being over written each time you iterate through the foreach loop. You would have to build an array to hold all of the animalids, and pass that to your modal or print out the id as you are iterating through the loop.

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.