我试图对DB返回的结果进行分页。但是当我试图从URI获得偏移时:
questions/search?content=foobar/4
/4
应该是偏移量,但会将其分配给$_GET
值。
这是控制器中的整个方法:
$results = $this->question->search_results_count($content);
$this->load->library('pagination');
$config['total_rows'] = count($results);
$offset = $this->uri->segment(3);
if ($offset == false) $offset = 0;
$config['full_tag_open'] = '<ul class="pages">';
$config['full_tag_close'] = '</ul>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['cur_tag_open'] = '<li><a class="active">';
$config['cur_tag_close'] = '</a></li>';
$config['prev_tag_open'] = '<li class="prev">';
$config['prev_tag_close'] = '<li>';
$config['next_tag_open'] = '<li class="next">';
$config['next_tag_close'] = '</li>';
$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';
$config['first_link'] = '<<';
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
$config['last_link'] = '>>';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';
$config['per_page'] = 1;
$config['uri_segment'] = 2;
$config['page_query_string'] = TRUE;
$config['use_page_numbers'] = TRUE;
$config['suffix'] = '?content='.$content;
$config['base_url'] = base_url().'questions/search/';
$this->pagination->initialize($config);
答案 0 :(得分:12)
我相信你知道,URI并不像那样工作。查询字符串必须位于末尾(或#
哈希片段之前)。此查询字符串:
questions/search?content=foobar/4
表示$_GET['content'] = 'foobar/4';
您需要将分页网址更改为以下内容:
questions/search/4/?content=foobar
4之后的/
也是可选的。
您必须从您的分页$config['base_url']
中删除查询字符串,然后将其附加到视图中的链接,这可能会导致黑客攻击分页类...
或者尝试这个未记录的功能:
// After loading the pagination class
$this->pagination->suffix = '{YOUR QUERY STRING}';
或者更好的是,只需在加载类之前将$config['suffix'] = '{YOUR QUERY STRING}';
添加到配置中。这应该会自动将查询字符串添加到每个链接的href
。
还需要对配置区域进行一些调整:
// Make sure to encode these
// $config['first_link'] = '<<';
$config['first_link'] = '<<';
// $config['last_link'] = '>>';
$config['last_link'] = '>>';
$offset = $this->uri->segment(3);
// Default URI segment is 3, and it's what you use above. Remove this.
// $config['uri_segment'] = 2;
// This should be FALSE (default). Remove it.
// $config['page_query_string'] = TRUE;
// This should be FALSE (default) if you're
// using the URI segment as your OFFSET. Remove it.
// $config['use_page_numbers'] = TRUE;
// Add your query string
$config['suffix'] = '?content='.$content;
$config['base_url'] = base_url().'questions/search/';
$this->pagination->initialize($config);