Pagnation with GET data in the uri - Codeigniter

2019-03-22 06:23发布

问题:

Im trying to paginate results returned by DB. But when I try to get the offset from URI:

questions/search?content=foobar/4

/4 should be the offset but it is assigned to the $_GET value.

This is the whole method in the controller:

http://pastebin.com/QFJddMDJ

$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);

回答1:

As I'm sure you know, URIs don't work like that. The query string must be at the end (or before a # hash fragment). This query string:

questions/search?content=foobar/4

Means $_GET['content'] = 'foobar/4';

You need to change your pagination URLs to something like this:

questions/search/4/?content=foobar

The / after the 4 there is also optional.

You'll have to remove the query string from your pagination's $config['base_url'] and instead append it to the links in the view, which sadly involves hacking the pagination class...

Or try this undocumented feature:

// After loading the pagination class
$this->pagination->suffix = '{YOUR QUERY STRING}';

Or better yet, just add $config['suffix'] = '{YOUR QUERY STRING}'; to your config before loading the class. This should automatically add the query string to every link's href.

Some adjustments to your config area also needed:

// Make sure to encode these
// $config['first_link'] = '<<';
$config['first_link'] = '&lt;&lt;';
// $config['last_link'] = '>>';
$config['last_link'] = '&gt;&gt;';

$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);