【发布时间】:2018-06-04 23:42:21
【问题描述】:
我是 CodeIgniter 的新手。我从 MySQL 获取数据并将其显示在 HTML 表中。 现在我需要删除 MySQL 中选定的行。
例如,假设表格有 10 行,每行都有一个复选框,并且表格上方有一个删除按钮。如果用户选择前五行并单击删除按钮,则应在 MySQL 数据库中删除这五行。然后 HTML 表数据将自动隐藏这些行,因为我已经从 MYSQL 表中获取数据。
如何做到这一点?
HTML 代码:
<div class="table-responsive">
<button id="export" data-export="export">Export</button>
<table id="export_table" class="table table-bordered">
<tr>
<th class="text-center">Created At</th>
<th class="text-center">Title</th>
<th class="text-center">Description</th>
<th class="text-center">Prority</th>
<th class="text-center">Status</th>
<th class="text-center">Start Date</th>
<th class="text-center">Due Date</th>
<th class="text-center">End Date</th>
<th class="text-center">By</th>
<th class="text-center">Additional Info</th>
</tr>
<?php foreach ($a->result() as $task) { ?>
<tr class="active">
<td><?php echo $task->time; ?></td>
<td><?php echo $task->title; ?></td>
<td><?php echo $task->description; ?></td>
<td><?php echo $task->priority; ?></td>
<td><?php echo $task->status; ?></td>
<td><?php echo $task->start_date; ?></td>
<td><?php echo $task->due_date; ?></td>
<td><?php echo $task->end_date; ?></td>
<td><?php echo $task->assigned_by; ?></td>
<td><?php echo $task->additional_info; ?></td>
</tr>
<?php } ?>
</table>
</div>
控制器代码:
public function user_profile() {
$this->load->model('user_model');
$data['a']=$this->user_model->all();
$this->load->view('user_profile', $data);
}
型号代码:
public function all() {
$query = $this->db->get('issues');
return $query;
}
【问题讨论】:
-
线索是,你可以先在你的表上添加复选框,然后确保你的表有一个id,每行cretae隐藏输入并用id设置值,你需要 javascript 或 jquery 来检查复选框的选中状态,如果它处于该状态并且您可以将该值 id 发送到控制器,在控制器内部创建一个处理删除操作的函数,您发送的 id 将是参数接收执行删除,不要忘记重新写入表 url
-
做这样的事情 $ids = array(4,5); $this->db->where_in('id', $ids); $this->db->delete('mytable');
标签: php html mysql forms codeigniter