我是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;
}
你可以使用Codeigniter的delete
函数来完成它
您可以通过表单sumbit / ajax调用将用户选择的行的ID传递给PHP。
然后使用delete
函数删除相应的行。
注意:确保在delete语句之前添加where子句以删除特定行。
示例代码
$this->db->where('id', $id);
$this->db->delete('issues');