【发布时间】:2015-03-20 14:36:38
【问题描述】:
我正在尝试使用查询字符串实现 Codeigniter 分页,但遇到了一些问题。我已经打开了
$config['page_query_string'] = TRUE;
所以要使用查询字符串进行分页,但据我所知,当您将查询字符串用于控制器和方法路由时,这确实适用。但是,在我的情况下,我仍在使用 URI 段进行路由,但只想使用查询字符串进行分页、过滤结果、搜索等。当我尝试使用 http_build_query() 使用通过它发送的查询字符串重建 url 时,会导致 per_page (我已将其重命名为偏移量)以在第一页之后的任何分页链接上写入两次。原因是当我重新创建查询字符串时,偏移量已经在后续页面的 $_GET 中,并且 CI 也在附加它,导致它出现两次。在下面的代码中,我从 $_GET 中删除了原始的 per_page 查询字符串,因此可以在没有它的情况下重建查询字符串,CI 将在分页 create_links() 期间添加它。我想检查这是否有意义,或者是否有更清洁的方法来处理这个问题。
// 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);
【问题讨论】:
-
你为什么使用查询字符串而不使用路由器?我有点不确定您要达到的目标。一个简单的解决方案是使用路由器但重载方法,可以这么说,您可以通过将默认值传递给方法来做到这一点。如果你想对浏览器隐藏 uri,只需通过 ajax 调用方法
-
因为过去我在使用 uri 段时遇到了问题,其中每页的项目数总是添加到 url 的末尾,我想将搜索参数作为密钥对发送,但是当您使用 uri_to_assoc 时,Codeigniter 在 url 末尾附加的每页值被视为键,因为它是单个 uri 段,不会被视为 /per_page/10。
标签: php codeigniter pagination codeigniter-2