单击CI分页链接会产生错误的查询

时间:2013-11-19 16:11:43

标签: codeigniter pagination

我在使用CodeIgniter点击我的网站的分页链接时遇到问题。搜索功能正常,但是,当我点击任何分页链接时,似乎它一次又一次地进行过滤。

例如:

我选择“租”作为我的搜索选择:

enter image description here

然后点击搜索我得到27个结果。之后,我点击底部的“2”分页按钮:

enter image description here

我得到另外73个结果,依此类推。

enter image description here

我的控制器:

$this->load->library('pagination');
$search['base_url'] = base_url() . 'page_search/';
$search['per_page'] = 5;
$search['num_links'] = 5;
$search['num_tag_open'] = '<li>';
$search['num_tag_close'] = '</li>';
$search['first_link'] = 'First';
$search['first_tag_open'] = '<li class="first">';
$search['first_tag_close'] = '</li>';
$search['last_tag_open'] = '<li class="last">';
$search['last_tag_close'] = '</li>';
$search['next_tag_open'] = '<li>';
$search['next_tag_close'] = '</li>';
$search['prev_tag_open'] = '<li>';
$search['prev_tag_close'] = '</li>';
$search['last_link'] = 'Last';
$search['next_link'] = '&raquo;';
$search['prev_link'] = '&laquo;';
$search['full_tag_open'] = '<div class="pagination pagination-centered"><ul>';
$search['full_tag_close'] = '</ul></div>';
$search['cur_tag_open'] = '<li class="active"><a href="#">';
$search['cur_tag_close'] = '</a></li>';
$search['total_rows'] = $this->db->get_where( 'listing', $where )->num_rows();
$search['uri_segment'] = 2;
$config['use_page_numbers'] = TRUE;

$this->pagination->initialize( $search );
$this->db->order_by("id", "desc");
$this->db->where( $where );
$data['results'] = $this->db->get( 'listing', $search['per_page'], $this->uri->segment(2);
$data['rows'] = $this->db->get_where( 'listing', $where )->num_rows();

$data['content'] = 'website/page-search';
$this->load->view('template/website/theme', $data);

哪个$在哪里查询我相信它可以正常工作。

更新

我的路线也是这样:

$route['page_search/(:num)'] = "page_search/index/$1";

以下是实际网站http://angkor21.com/

的链接

1 个答案:

答案 0 :(得分:1)

您正在发布搜索表单并发表$where声明,这很酷,但在第2页中,您的$where声明是否正常工作?我认为这是你需要注意的地方。在第二页中打印生成的查询,如下所示:

$this->db->last_query();

您需要以某种方式将这些search parameter(s)传递到下一页,以正确生成$where条件。例如:

if( $this->input->post(null) ){     #if the form is submitted
    $saleType   = $this->input->post('sale_type');
    $propType   = $this->input->post('prop_type');
    $city       = $this->input->post('city');
    $district   = $this->input->post('district');
    $commune    = $this->input->post('commune');    
}else{
    $saleType   = $this->uri->segment(3);
    $propType   = $this->uri->segment(5);
    $city       = $this->uri->segment(7);
    $district   = $this->uri->segment(9);
    $commune    = $this->uri->segment(11);
}


$search['base_url']     = base_url() . 'page_search/sale_type/'.$saleType.'/prop_type/'.$propType.'/city/'.$city.'/district/'.$district.'/commune/'.$commune.'/page/';
$search['uri_segment']  = 13;