从数据库返回相同的值到bootstrap模式

时间:2016-02-22 05:47:13

标签: php jquery twitter-bootstrap codeigniter codeigniter-2

我的数据显示在表格中,编辑和删除数据的方式是bootstrap模式。我只是展示我的删除模式。我使用CodeIgniter 2.2.6:

我的表:

<table class="table table-striped table-bordered table-hover">
<thead class="text-center">
    <th class="text-center">No.</th>
    <th class="text-center">Major Code</th>
    <th class="text-center">Major Name</th>
    <th class="text-center">Option</th>
</thead>
<tbody>
    <?php $no = 1; ?>
    <?php foreach ($select_major as $row): ?>
        <tr>
            <td><?php echo $no++;               ?></td>
            <td><?php echo $row->cd_major;      ?></td>
            <td><?php echo $row->name_major;    ?></td>
            <td align=center>
                <!-- DELETE BUTTON -->
                <a
                    class="btn btn-danger btn-xs"
                    role="button"
                    data-toggle="modal"
                    data-target="#modal-delete"
                    href="<?php
                        $cd_major   = $row->cd_major;
                        $name_major = $row->name_major;
                    ?>">
                    <i class="fa fa-fw fa-trash"></i> Delete
                </a>
            </td>
        </tr>
    <?php endforeach; ?>
</tbody>

我的模态:

<!-- MODAL DELETE DATA -->
<div class="modal fade" id="modal-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form class="form-horizontal" method="POST" action="<?php echo base_url('major'); ?>">
                <input type="hidden" name="id_major" value="<?php echo (isset($id_major) ? $id_major : ''); ?>" />
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>

                    <!-- MODAL TITLE -->
                    <h4 class="modal-title"><i class="fa fa-fw fa-trash"></i> Delete Data</h4>
                </div>

                <!-- FORM -->
                <div class="modal-body">
                    <p>Are you sure to delete this major? <b><?php echo $name_major; ?></b> ini?</p>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
                    <input type="submit" class="btn btn-danger" name="delete" value="Yes, delete" />
                </div>
            </form>
        </div><!--/ .modal-content -->
    </div><!--/ .modal-dialog -->
</div><!--/ .modal -->

但是当我尝试删除所选数据时,它总是会命令删除最后一个数据。我该怎么做才能解决它?

1 个答案:

答案 0 :(得分:1)

在您的情况下,您将添加如下代码:

你的新表:

<table class="table table-striped table-bordered table-hover">
<thead class="text-center">
    <th class="text-center">No.</th>
    <th class="text-center">Major Code</th>
    <th class="text-center">Major Name</th>
    <th class="text-center">Option</th>
</thead>
<tbody>
    <?php $no = 1; ?>
    <?php foreach ($select_major as $row): ?>
        <tr>
            <td><?php echo $no++;               ?></td>
            <td><?php echo $row->cd_major;      ?></td>
            <td><?php echo $row->name_major;    ?></td>
            <td align=center>
                <!-- DELETE BUTTON -->
                <a
                    class="btn btn-danger btn-xs"
                    role="button"
                    data-toggle="modal"
                    data-target="#modal-delete"
                    href="<?php
                        $cd_major   = $row->cd_major;
                        $name_major = $row->name_major;
                    ?>" onclick=deleteRecord(<?php $row->cd_major?>)>
                    <i class="fa fa-fw fa-trash"></i> Delete
                </a>
            </td>
        </tr>
    <?php endforeach; ?>
</tbody>
</table>
<script>
function deleteRecord(id){
    document.getElementById("id_major").value = id;
}
</script>

你的新模式:

<!-- MODAL DELETE DATA -->
<div class="modal fade" id="modal-delete" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <form class="form-horizontal" method="POST" action="<?php echo base_url('major'); ?>">
                <input type="hidden" name="id_major" id="id_major" value="<?php echo (isset($id_major) ? $id_major : ''); ?>" />
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>

                    <!-- MODAL TITLE -->
                    <h4 class="modal-title"><i class="fa fa-fw fa-trash"></i> Delete Data</h4>
                </div>

                <!-- FORM -->
                <div class="modal-body">
                    <p>Are you sure to delete this major? <b><?php echo $name_major; ?></b> ini?</p>
                </div>

                <div class="modal-footer">
                    <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
                    <input type="submit" class="btn btn-danger" name="delete" value="Yes, delete" />
                </div>
            </form>
        </div><!--/ .modal-content -->
    </div><!--/ .modal-dialog -->
</div><!--/ .modal -->

请使用我给你的代码,现在应该可以使用。

我假设cd_major是您要删除的行的ID,如果没有,您可以替换我们在onclick事件中添加到<a>标记的函数中的字段名称。

由于