我有几页数据,是从数据库中提取出来的。我需要能够删除页面上显示所有数据行的记录。
因此customers/index显示所有行。控制器:
public function index($page = 'customer_list', $title = 'Customer List') {
$this->exists($page);
$data['customer'] = $this->customers_model->get_customers();
$data['title'] = $title;
$data['content'] = 'content/'.$page.'.php';
$this->load->view('frame/grab', $data);
}
public function delete() {
$this->customers_model->delete_customer();
$this->index();
}
它使用此模型函数:
public function get_customers() {
$this->load->library('pagination');
//Config for pagination
$config['base_url'] = '**cant post links**/customers/index';
$config['total_rows'] = $this->db->get('customers')->num_rows();
//Rows per page
$config['per_page'] = 10;
//Number of links shown to navigate
$config['num_links'] = 20;
//Initializes the pagination
$this->pagination->initialize($config);
$query = $this->db->get('customers', $config['per_page'], $this->uri->segment(3));
return $query->result();
}
我的观点是:
<div class="main-content">
<div class="main-content-header">
<h2 class="floatl"><?php echo $title; ?></h2>
<a class="add-new floatr" href="<?php echo base_url(); ?>customers/add"><h4>Add New Customer »</h4></a>
<div class="clear"></div>
</div>
<div class="sort-container">
Sort by:
<ul class="sort">
<li><a href="#">ID Ascending</a></li>
<li><a href="#">ID Descending</a></li>
<li><a href="#">First Name</a></li>
<li><a href="#">Last Name</a></li>
</ul>
</div>
<?php
//Creates the template for which the table is built to match
$tmpl = array ( 'table_open' => '<table class="data-table">
<tr class=\'tr-bgcolor table-header\'>
<td>#</td>
<td>First Name</td>
<td>Last Name</td>
<td>Edit</td>
<td>Delete</td>
</tr>',
'heading_row_start' => '<tr>',
'heading_row_end' => '</tr>',
'heading_cell_start' => '<th>',
'heading_cell_end' => '</th>',
'row_start' => '<tr class="tr-bgcolor">',
'row_end' => '</tr>',
'cell_start' => '<td>',
'cell_end' => '</td>',
'row_alt_start' => '<tr>',
'row_alt_end' => '</tr>',
'cell_alt_start' => '<td>',
'cell_alt_end' => '</td>',
'table_close' => '</table>'
);
$this->table->set_template($tmpl);
//Creates table rows from the rows of data in the db which were created as the customer array
foreach ($customer as $row) :
$this->table->add_row(
$number = array('data' => $row->rec_num, 'class' => 'center'),
$row->last_name,
$row->first_name,
$edit = array('data' => '[ <a href="#">Edit</a> ]'),
$edit = array('data' => '[ <a href="'.base_url().'customers/delete/'.$row->rec_num.'">Delete</a> ]')
);
endforeach;
//Creates the table based on the rows determined above
echo $this->table->generate();
//Creates page links
echo $this->pagination->create_links();
?>
</div>
所以我认为分页会妨碍我,因为当我单击delete链接时,它会将其发送给customers/delete/#。所以它会按原样删除记录,但显示的结果是不同的,因为我相信分页是按URL中的数字(实际上只是行的ID)来设置显示的行的。
任何帮助都非常感谢。
最佳答案
我认为最好的方法是将数据库中的行删除后重定向到索引页:
public function delete() {
$this->customers_model->delete_customer();
redirect('customers/index');
}