PHP Foreach循环将数据传递到jquery模式以供表单使用

时间:2015-02-06 22:11:04

标签: php jquery twitter-bootstrap modal-dialog

我正在使用带有Codeigniter的Bootstrap。我有一个表,可以在我构建的管理界面上加载帐户的块历史记录。在作为foreach的一部分的每一行上,有一个&#34; unblock&#34;按钮,如果用户可以删除该块,您希望在下面的示例中看到。该按钮具体为<td><?php if ($bd['expiredate'] > date('Y-m-d H:i:s')) { ?><button type="button" class="btn btn-info" data-toggle="modal" data-target="#delBlock" <?php if ($check_perm['unbanaccount'] == 0 || $bd['unblock_date'] > 0) { echo "disabled"; } ?> >Unblock</button><?php } ?></td>

        <div class="table-responsive">
            <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                <thead>
                    <tr>
                        <th style="width: 125px;">Block Date</th>
                        <th style="width: 100px;">Blocked By</th>
                        <th style="width: 125px;">Expiry Date</th>
                        <th style="width: 100px;">Reason</th>
                        <th style="width: 100px;">Unblocked By</th>
                        <th style="width: 125px;">Unblocked Date</th>
                        <th style="width: 30px;">Block<br />Comment</th>
                        <th style="width: 30px;">Unblock<br />Comment</th>
                        <th style="width: 100px;">Options</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($block_list)) { echo "<tr class='odd gradeX'><td colspan='9'><center>No data!</center></td></tr>"; } ?>
                    <?php foreach ($block_list as $bd): ?>
                        <tr class="odd gradeX">
                            <td><?php echo $bd['blockdate']; ?></td>
                            <td><?php echo $bd['blockname']; ?></td>
                            <td><?php echo $bd['expiredate']; ?></td>
                            <td><?php echo $bd['reason']; ?></td>
                            <td><?php echo $bd['ublockname']; ?></td>
                            <td><?php echo $bd['unblock_date']; ?></td>
                            <td><center><a data-toggle="collapse" data-parent="#accordion" href="#blockcomment<?php echo $bd['blockid']; ?>"><button type="button" class="btn btn-primary btn-circle"><i class="fa fa-list"></i></button></a></center></td>
                            <td><?php if (isset($bd['unblock_date']) == TRUE) { ?><center><a data-toggle="collapse" data-parent="#accordion" href="#ublockcomment<?php echo $bd['blockid']; ?>"><button type="button" class="btn btn-primary btn-circle"><i class="fa fa-list"></i></button></a></center><?php } ?></td>
                            <td><?php if ($bd['expiredate'] > date('Y-m-d H:i:s')) { ?><button type="button" class="btn btn-info" data-toggle="modal" data-target="#delBlock" <?php if ($check_perm['unbanaccount'] == 0 || $bd['unblock_date'] > 0) { echo "disabled"; } ?> >Unblock</button><?php } ?></td>
                        </tr>
                        <tr><td colspan="9">
                            <div id="blockcomment<?php echo $bd['blockid']; ?>" class="panel-collapse collapse">
                                <div class="panel-body">
                                    <strong>Block Comment:</strong><br /><?php echo $bd['block_comment']; ?>
                                </div>
                            </div>
                            <div id="ublockcomment<?php echo $bd['blockid']; ?>" class="panel-collapse collapse">
                                <div class="panel-body">
                                    <strong>Unblock Comment:</strong><br /><?php echo $bd['unblock_comment']; ?>
                                </div>
                            </div>
                        </td></tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            <div align="right"><button type="button" class="btn btn-warning" data-toggle="modal" data-target="#addBlock" <?php if ($check_perm['banaccount'] == 0) { echo "disabled"; } ?> >Add New Block</button></div>
        </div>

点击&#34;取消阻止&#34;如果帐户可以解锁,我会有一个模式,您可以在其中输入关于您取消阻止用户的评论。每个块都由数据库中的唯一ID(上例中的变量为$bd['blockid'])引用。正是这个blockid我需要与数据库进行通信并告诉它更新数据的块,以表明用户在表上被解锁。

模态如下:

        <div class="modal fade" id="delBlock" tabindex="-1" role="dialog" aria-labelledby="delBlockLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                        <h4 class="modal-title" id="delBlockLabel">Remove Block</h4>
                    </div>
                    <div class="modal-body">
                        <?php echo validation_errors(); ?>
                        <?php echo form_open('/account/delblock', array('class' => 'form-inline'), array('blockid' => $bd['blockid'], 'acct_id' => $acct_data->account_id)); ?>
                        <table>
                            <tr><td width="25%"><label>Unblock Comment</label></td>
                            <td width="450px"><textarea class="form-control" name="unbanComments" rows="5" style="width:100%;"></textarea></td></tr>
                        </table>
                    </div>
                    <center><div style="color:#EE0000; ">Note this will remove the ban with immediate effect.</div></center>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                        <button type="submit" class="btn btn-primary">Remove Block</button>
                    </div>
                    <?php echo form_close(); ?>
                </div>
            </div>
        </div>

正如您所看到的,我使用Codeigniter的表单助手功能在表单中发布array('blockid' => $bd['blockid'],,以便我可以使用我的Codeigniter模型操作该记录。但是,由于jQuery模式不在循环中,它总是将最后一条记录作为$ bd [&#39; blockid&#39;]而不是我想要操作的实际记录(在这种情况下,我有7,6和5的ID(按此顺序),我点击ID 7的记录,而是改变5)。我需要将特定$ bd [&#39; blockid&#39;]的模式传递给表单,以便我可以在我的Codeigniter模型中使用它。我将如何完成这样的事情?

1 个答案:

答案 0 :(得分:0)

我做了一些事情,将一些东西拼凑在一起并想出了这个:

视图我将按钮更改为:

<td><?php if ($bd['expiredate'] > date('Y-m-d H:i:s')) { ?><button type="button" class="btn btn-info" id="delBlockOpen" data-toggle="modal" data-target="#delBlock" data-id="<?php echo $bd["blockid"]; ?>" <?php if ($check_perm['unbanaccount'] == 0 || $bd['unblock_date'] > 0) { echo "disabled"; } ?> >Unblock</button><?php } ?></td>

(已添加data-id="<?php echo $bd["blockid"]; ?>"

然后,在模态中:

<div class="modal fade" id="delBlock" tabindex="-1" role="dialog" aria-labelledby="delBlockLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="delBlockLabel">Remove Block</h4>
            </div>
            <div class="modal-body">
                <?php echo validation_errors(); ?>
                <?php echo form_open('/account/delblock', array('class' => 'form-inline'), array('acct_id' => $acct_data->account_id)); ?>
                <input type="hidden" id="blockidval" name="blockidval" />
                <table>
                    <tr><td width="25%"><label>Unblock Comment</label></td>
                    <td width="450px"><textarea class="form-control" name="unbanComments" rows="5" style="width:100%;"></textarea></td></tr>
                </table>
            </div>
            <center><div style="color:#EE0000; ">Note this will remove the ban with immediate effect.</div></center>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary">Remove Block</button>
            </div>
            <?php echo form_close(); ?>
        </div>
    </div>
</div>

最后,我的页脚中补充了一些额外的javascript:

<script type="text/javascript">
  $(function() {
      $(document).on('click','#delBlockOpen',function(e){
          $('#blockidval').val($(this).data('id'));
      });
  });
</script>