简体   繁体   English

使用查询字符串的Codeigniter分页

[英]Codeigniter pagination using query strings

I am trying to implement Codeigniter pagination using query strings but have run into a few issues with this. 我正在尝试使用查询字符串实现Codeigniter分页,但是遇到了一些问题。 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. 但是,在我的情况下,我仍然使用URI段进行路由,但只想使用查询字符串进行分页,过滤结果,搜索等。当我尝试使用http_build_query()来构造带有通过其发送的查询字符串的url时,会导致per_page (我将其重命名为offset)以在第一页之后的任何分页链接上写入两次。 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. 原因是当我重新创建查询字符串偏移量时,该偏移量已经在后续页面的$ _GET中,并且CI也附加了它,导致它出现两次。 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(). 在下面的代码中,我从$ _GET中删除了原始的per_page查询字符串,因此可以在没有该字符串的情况下重建查询字符串,CI会在分页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. 使用分页库的CodeIgniter 3.0版本。 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. 我已经在CodeIgniter 2中实现了它,但没有取代分布式版本,而是将其部署为名为MY_Pagination的重载库并将其放置在“应用程序/库”文件夹中。 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. 为了使这种方式生效,我要做的唯一代码更改就是将访问修饰符设置为public而不是protected。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM