php - 需要向 html 表添加一个删除按钮,以通过一条消息从数据库中删除记录

标签 php html mysql html-table sql-delete

<table class="table admin-table list-table nurserytwo-table">
<thead>
    <tr>
        <td>ID</td>
        <td>Name</td>
        <td>Code</td>
        <td>Active</td>
        <td>Edit</td>
    </tr>
</thead>
<tbody>
    <?php foreach($nurseries->result() as $nursery) { ?>
    <tr>
        <td><?php echo $nursery->id; ?></td>
        <td><?php echo $nursery->name; ?></td>
        <td><?php echo $nursery->code; ?></td>
        <td><?php echo set_bool($nursery->active); ?></td>
        <td><span class="glyphicon glyphicon-edit"></span> <?php echo 
anchor('admin/nurseries/edit_nursery/'.$nursery->id, 'Edit', 'class="edit-
link"'); ?></td>
    </tr>
    <?php } ?>
</tbody>
</table>

这是我当前的表代码,我已经仔细查看了周围,看看我是否可以找出如何做到这一点,但我对 php 真的很陌生,似乎无法理解它。我需要在编辑按钮后添加一个删除按钮,我知道我可以通过 delete.php 来完成此操作,但不知道从哪里开始。任何帮助将不胜感激。

更新:

这是我目前拥有的:

      }elseif( $action == "delete_nursery_course" ){

        if($id) {

            $q = $this->db->where('id', $id)->delete('nursery_courses');
            if($this->db->affected_rows() > 0) {
                if($this->input->is_ajax_request()) {
                    $this->output->enable_profiler(FALSE);
                    echo "SUCCESS"; die();
                }else{
                    set_flash_message('Nursery deleted successfully.', 
'success');
                }
            }else{
                if($this->input->is_ajax_request()) {
                    $this->output->enable_profiler(FALSE);
                    echo "Something went wrong. Please try again."; die();
                }else{
                    set_flash_message('Something went wrong. Please try 
again.', 'error');
                }
            }

这是 html:

<table class="table admin-table list-table nurseryone-table">

<thead>
    <tr>
        <td>ID</td>
        <td>Name</td>
        <td>Colour</td>
        <td>Active</td>
        <td>Edit</td>
        <td>Delete</td>
    </tr>
</thead>
<tbody>
    <?php foreach($nursery_courses->result() as $nursery_course) { ?>
    <tr>
        <td><?php echo $nursery_course->id; ?></td>
        <td><?php echo $nursery_course->name; ?></td>
        <td><div style="background:<?php echo $nursery_course->colour; ?>" 
class="course-colour"></div></td>
        <td><?php echo set_bool($nursery_course->active); ?></td>
        <td><span class="glyphicon glyphicon-edit"></span> <?php echo 
 anchor('admin/nurseries/edit_nursery_course/'.$nursery_course->id, Edit', 'class="edit-link"'); ?></td>
        <td>  
<span class="glyphicon glyphicon-trash"></span> <?php echo 
anchor('admin/nurseries/delete_nursery_course/'.$nursery_course->id, 
'delete', 'class="delete-link"'); ?></td>
    </tr>   
    <?php } ?>
</tbody>
</table>

最佳答案

您只能使用 html 和 javascript 以及 jquery 来使用 ajax 请求来创建编辑和删除按钮。

HTML:

<td class="text-right">
    <a class="edit btn btn-sm btn-default" href="javascript:;">
         <i class="icon-note"></i>
    </a>
    <a class="delete btn btn-sm btn-danger" href="javascript:;">
        <i class="icons-office-52"></i>
    </a>
</td>

并且在您的 javascript 文件中,您必须为此按钮定义 onclick 事件。

这是一个包含 html 和 javascript 代码的 Plunker 示例: Editable Table

您还可以使用 jquery DataTables 使其变得更容易、更好。

希望对您有帮助。

关于php - 需要向 html 表添加一个删除按钮,以通过一条消息从数据库中删除记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45663900/

相关文章:

php - 在 PHP 中关闭选项卡或离开站点调用 session __destruct 吗?

php - 使用 MySQL 直接从列的数据中获取列表

php - Yii2:条件验证器总是需要返回

javascript - 使用 Chrome Ember.js 打开 URL 时无法使 HTML anchor 正常工作

javascript - 如何在视频上叠加另一个 div?

mysql - Node js-mysql插入查询中如何获取插入的行数据?

php - 如何使用 php 更改 Bootstrap 列响应式布局?

javascript - 第二个范围 slider 在 html 中不起作用

php - 如何从数据库中放入 <select> 类别和子类别?

mysql - 如何在 mysql 查询中放置 order by 和 group by?