我在我的 CI 项目中实现了这个带有搜索结果的分页,但是我无法让我的分页链接转到下一页,例如:
我第一次加载时的搜索页面是http://localhost/ci_project/search
,当我点击将转到的分页链接时http://localhost/ci_project/search/2
,页面就会显示出来404 Page Not Found
。
有人可以帮我弄清楚我的代码有什么问题吗?我尝试了很多关于解决方案的建议,但无济于事,大部分问题来自$config['base_url'] = base_url('search');
,但没有一个是帮助。
模型:
public function do_search_count($keywords)
{
$sql = "SELECT COUNT(*) AS cnt FROM products WHERE MATCH (name, manufacturer) AGAINST ('".$keywords."')";
$q = $this->db->query($sql);
$row = $q->row();
return $row->cnt;
}
public function do_search($keywords, $per_page, $limit)
{
$this->db->escape($keywords);
$this->db->where('MATCH (name, manufacturer) AGAINST ("'.$keywords.'") LIMIT '.$per_page.', '.$limit, NULL, FALSE);
$query = $this->db->get('products');
if($query->num_rows() > 0){
return $query->result();
}else{
return false;
}
}
控制器:
function search()
{
$keywords = $this->input->post('search');
$searchterm = $this->db_model->searchterm_handler($this->input->get_post('search', TRUE));
$limit = ($this->uri->segment(2) > 0)?$this->uri->segment(2):0;
$config['base_url'] = base_url('search');
$config['total_rows'] = $this->db_model->do_search_count($searchterm);
$config['per_page'] = 5;
$config['uri_segment'] = 2;
$config['use_page_numbers'] = TRUE;
$choice = $config['total_rows'] / $config['per_page'];
$config['num_links'] = round($choice);
$this->pagination->initialize($config);
$data['results'] = $this->db_model->do_search(trim($keywords), $limit, $config['per_page']);
$data['pagination'] = $this->pagination->create_links();
$data['searchterm'] = $searchterm;
$data['total'] = count($data['results']);
$data['title'] = 'Search';
$this->load->view('header', $data);
$this->load->view('search', $data);
$this->load->view('footer', $data);
}
谢谢。