Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用查询字符串的Codeigniter分页_Php_Codeigniter_Pagination_Codeigniter 2 - Fatal编程技术网

Php 使用查询字符串的Codeigniter分页

Php 使用查询字符串的Codeigniter分页,php,codeigniter,pagination,codeigniter-2,Php,Codeigniter,Pagination,Codeigniter 2,我试图使用查询字符串实现Codeigniter分页,但遇到了一些问题。我已经打开了 $config['page_query_string'] = TRUE; 因此,使用查询字符串进行分页,但据我所知,当您使用查询字符串进行控制器和方法路由时,这确实是可行的。然而,在我的例子中,我仍然使用URI段进行路由,但只想使用查询字符串进行分页、筛选结果、搜索等。当我尝试使用http_build_query()用通过它发送的查询字符串重建url时,会导致per_页面(我已将其重命名为offset)在第一页

我试图使用查询字符串实现Codeigniter分页,但遇到了一些问题。我已经打开了

$config['page_query_string'] = TRUE;
因此,使用查询字符串进行分页,但据我所知,当您使用查询字符串进行控制器和方法路由时,这确实是可行的。然而,在我的例子中,我仍然使用URI段进行路由,但只想使用查询字符串进行分页、筛选结果、搜索等。当我尝试使用http_build_query()用通过它发送的查询字符串重建url时,会导致per_页面(我已将其重命名为offset)在第一页之后的任何分页链接上写入两次。原因是,当我重新创建查询字符串时,偏移量已经在后续页面上的$\u GET中,CI也在追加它,导致它出现两次。在下面的代码中,我已从$\u GET中删除了原始的每页查询字符串,因此可以在不使用该字符串的情况下重建查询字符串,CI将在分页创建链接()期间添加该字符串。我想看看这是否有道理,或者是否有一种更干净的方法来处理这个问题

// 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);
使用分页库的版本。它有一个可重用查询字符串的配置选项


我自己在CodeIgniter 2中实现了它,但我没有替换分布式版本,而是将它部署为一个名为MY_Pagination的重载库,并将其放置在我的“application/libraries”文件夹中。我必须做的唯一代码更改就是将访问修饰符设置为public而不是protected

为什么使用查询字符串而不使用路由器?我有点不确定你想要实现什么。一个简单的解决方案是使用路由器,但会使方法过载,也就是说,您可以通过将默认值传递到方法中来实现这一点。如果您想从浏览器中隐藏uri,只需通过Ajax调用该方法,因为在过去,我在使用uri段时遇到问题,每页的项目数总是添加到url的末尾,并且我希望将搜索参数作为键对发送,但Codeigniter在url末尾附加的每页值被视为当您使用uri_to_assoc时,它是一个单独的uri段,不会被视为/per_page/10。我在CI3更改日志中看到了这一点。我来看看这个。