首页 > 解决方案 > 在 php foreach 函数中单击按钮时按 id 显示模式

问题描述

我在phpforeach的函数中有一个按钮。我有一个模式,其id对应于单击的按钮。现在,我想要做的是,当我单击按钮时,它会按 id 显示模态,但第一个按钮只能显示模态,而另一个按钮则不能。我应该怎么办?这是我的代码...

           <?php $count = 0;  ?>
                <?php foreach ($land_i as $li){ ?>
                <?php $count++ ?>
                    <tr>                             
                    <td><?= $li['date_approved'] ?></td>
                    <td><?= $li['status'] ?></td>
                    <td><button type="button" class="btn btn-primary" data-toggle="modal" data-target=".modal_<?= $li['lpf_no'] ?><?= $count ?>" data-backdrop="static" data-keyboard="false">Select</button>
                    </td>
                    </tr>
          <?php } ?>

我尝试使用count从按钮生成它的唯一编号。我知道这是无意义的,因为每个按钮已经有一个唯一的 id。这是我的模态...

                <div class="modal fade modal_<?= $li['lpf_no'] ?><?= $count ?>" tabindex="-1" role="dialog" aria-hidden="true" style="display: none;">
                <div class="modal-dialog modal-lg">
                  <div class="modal-content">

                    <div class="modal-header">
                      <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span>
                      </button>
                      <h4 class="modal-title" id="myModalLabel">In Progress...</h4>
                    </div>
                    <div class="modal-body">
                      <h4><?= $li['lpf_no'] ?></h4>
                    </div>
                    <div class="modal-footer">
                      <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                      <button type="button" class="btn btn-primary">Save changes</button>
                    </div>

                  </div>
                </div>
              </div>

而已。希望有人能帮助我这个。这是另一件事......我尝试将模态放入我的foreach函数中。它有效,但模式破坏和页面设计也是如此。所以我更喜欢把它放在 foreach 函数之外。

标签: javascriptphpjquery

解决方案


The problem is in your data-target. You append the count on to the end of the data-target in the loop (data-target=".modal_<?= $li['lpf_no'] ?><?= $count ?>), along with your modal itself: <div class="modal fade modal_<?= $li['lpf_no'] ?><?= $count ?> ... </div>. The problem is that $count does not reflect the current count; the loop has already finished by this point; $count now only refers to the last item.

Assuming you want a different modal for each item, you will indeed need to output the modals inside of a loop -- I'd actually recommend making use of a second loop for this, so as not to have to worry about things like closing your table:

<table>
<?php $count = 0; ?>
<?php foreach ($land_i as $li) { ?>
    <?php $count++ ?>
    <tr>                             
    <td><?= $li['date_approved'] ?></td>
    <td><?= $li['status'] ?></td>
    <td><button type="button" class="btn btn-primary" data-toggle="modal" data-target=".modal_<?= $li['lpf_no'] ?><?= $count ?>" data-backdrop="static" data-keyboard="false">Select</button>
    </td>
    </tr>
<?php } ?>
</table>

<?php $count = 0; ?>
<?php foreach ($land_i as $li) { ?>
    <?php $count++ ?>
    <div class="modal fade modal_<?= $li['lpf_no'] ?><?= $count ?>" tabindex="-1" role="dialog" aria-hidden="true" style="display: none;">
        ...
    </div>
<?php } ?>

This way you will end up with a different modal for each item, and they will all have the correct data-target reference. It also shouldn't break your formatting due to the closure of the <table> before starting the second loop.


推荐阅读