Codeigniter分页对我不起作用。它显示页码和前5个搜索结果,但当我点击页面'2'时,它会加载'search_nok'视图,并显示消息“请选择您的选项”。您可以查看下面的代码并帮我找出错误。 这是我的控制器:
public function search($offset = 0) {
$limit = 5;
$this->load->library('form_validation');
$this->load->model('model_x');
$this->form_validation->set_rules('country', 'Country','required');
if($this->form_validation->run()) {
$country = $this->input->post('country');
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/abc/cont/search/'; //where 'http://localhost/abc' is my base url
$config['total_rows'] = 14;
$config['per_page'] = 5;
$data['pagination'] = $this->pagination->initialize($config);
if ($this->model_x->did_search($country, $limit, $offset)){
$data["results"] = $this->model_x->did_search($country, $limit, $offset);
$this->load->view("search_ok",$data);
}
}
else
{
$data['message'] = 'Please select your options.';
$this->load->view("search_nok",$data);
}
}
以下是我的观点:
<?php
echo $this->pagination->create_links();
foreach($results as $row){
$country = $row['country'];
$city = $row['city'];
?>
<table>
<tr>
<td >
<b><?php echo $country; ?></b>
</td>
<td>
<b><?php echo $city; ?></b>
</td>
</tr>
</table>
<?php }?>
答案 0 :(得分:1)
我认为你对分页感到困惑。首先,技术上所有的分页内容:
<?php
$this->load->library('pagination');
$config['base_url'] = 'http://localhost/abc/cont/search/'; //where 'http://localhost/abc' is my base url
$config['total_rows'] = 14;
$config['per_page'] = 5;
应该在您的控制器中,其次,您实际上应该限制模型中的结果,因为您还没有显示您的模型我会想出一个示例
$this->db->limit ($limit $offset //these will be defined in your controller)
$data = $this->db->get('whatever_table')->whatever_result_type.
return $data
您应该在控制器中定义$limit
并将其作为函数接受的参数传递给您的模型函数
控制器还应该有$ offset和$ limit defined
public function search($offset = 0) {
$limit = 5;
当你打电话给模特时,一定要通过
$this->model_x->did_search($country, $limit, $offset);
然后而不仅仅是$this->pagination->initialize($config);
这样做(仍然在你的控制器中记住):
$data['pagination'] = $this->pagination->initialize($config);
然后在视图中的任何地方回应分页