【发布时间】:2011-12-03 16:56:05
【问题描述】:
我有从数据库中提取的数据页。我需要能够删除显示所有数据行的页面上的记录。
因此 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>
所以我认为分页妨碍了,因为当我单击删除链接时,它会将其发送给客户/删除/#。所以它应该删除记录,但显示的结果是多种多样的,因为我相信分页是通过 URL 中的数字设置显示的行(这实际上只是行的 ID)。
非常感谢任何帮助。
【问题讨论】:
-
delete_customer()在没有数字传递的情况下如何工作? -
对不起,我忽略了在模型中发布我的删除功能。它只是使用 URL 的第 3 段来获取行的 ID。所以如果是customers/delete/5,它将删除数据库中具有该数字的第三行。它还被用来抵消表中显示的记录,这就是问题所在。只是不确定我猜这通常是如何处理的。
-
模型不应该引用数据库和它给出的变量以外的任何东西。所以在Controller中你应该调用
$this->customers_model->delete_customer($id);,然后在模型中使用delete_customer($id)作为ID,而不是依赖模型中的URL。您可以使用控制器方法function delete($id)而不是function delete()获取 ID。 -
太好了,谢谢。我一定会调整我的代码。
标签: php mysql codeigniter pagination