I am trying to implement Codeigniter pagination using query strings but have run into a few issues with this. I've switched on
$config['page_query_string'] = TRUE;
So to use query strings for pagination but as far as I can see this is really intended to work when you are using query strings for controller and method routing. However in my case I am still using URI segments for routing but just want to use query strings for pagination, filtering results, search etc. When I try to use http_build_query() to reconstruct the url with the query string sent through it causes the per_page (which I have renamed to offset) to get written twice on any pagination link after the first page. The reason being that when I recreate the query string offset is already in the $_GET on subsequent pages and CI is also appending it as well causing it to appear twice. In the code below I've removed the original per_page query string from the $_GET so the query string can be rebuilt without it and CI will add this during the pagination create_links(). I wanted to check if this makes sense or if there is a cleaner way of dealing with this.
// load pagination library
$this->load->library('pagination');
// set pagination base url
$config['base_url'] = base_url('accounting/bank/reconcile-account1/account/' . $bank_account_id) . '/?';
// assign current $_GET parameters to local variable as we need to remove offset each time we rebuild query
// string otherwise it gets appended twice to url
$get = $_GET;
// unset the offset array item
unset($get['offset']);
// build first url link
$config['first_url'] = base_url('accounting/bank/reconcile-account1/account/' . $bank_account_id) . '/?' . http_build_query($get);
// if $get contains items then build these back onto the url
if (count($get) > 0) $config['suffix'] = '&' . http_build_query($get);
// set the total number of rows
$config['total_rows'] = $result['total_num_txns'];
// set the number of items per page
$config['per_page'] = $filter->limit;
// initialise the pagination config
$this->pagination->initialize($config);
Use the CodeIgniter 3.0 version of the Pagination Library. It has a config option to reuse the query string.
I've implemented it myself in a CodeIgniter 2, but rather than replace the distributed version , I deployed it as an overloaded library called MY_Pagination and placed it in my 'application/libraries' folder. The only code change I had to do to make this work this way was to set the access modifiers to public rather than protected.