简体   繁体   English

将PHP foreach中的变量添加到引导模式

[英]Add Variable from PHP foreach to bootstrap modal

I have a button within a php foreach statement which loads a modal. 我在php foreach语句中有一个加载模式的按钮。 How can i pass the particular id ($animalid) to the modal? 如何将特定的id($ animalid)传递给模态? I have it kind of working but the modal will load the same id in each modal popup. 我有某种工作方式,但模式会在每个模式弹出窗口中加载相同的ID。 See my php foreach code below and also part of the modal. 请参阅下面的我的php foreach代码以及模式的一部分。

$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;
        ?>

$animalid is in the loop. $animalid在循环中。 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:

<?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: 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>

$animalid is being over written each time you iterate through the foreach loop. 每次迭代foreach循环时, $animalid都会被覆盖。 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. 您将必须构建一个数组来保存所有动物标识,并将其传递给模态或在遍历循环时打印出该标识。

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

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