簡體   English   中英

使用查詢字符串的Codeigniter分頁

[英]Codeigniter pagination using query strings

我正在嘗試使用查詢字符串實現Codeigniter分頁,但是遇到了一些問題。 我已經開機

$config['page_query_string'] = TRUE;

因此,使用查詢字符串進行分頁,但據我所知,這實際上是在將查詢字符串用於控制器和方法路由時有效的。 但是,在我的情況下,我仍然使用URI段進行路由,但只想使用查詢字符串進行分頁,過濾結果,搜索等。當我嘗試使用http_build_query()來構造帶有通過其發送的查詢字符串的url時,會導致per_page (我將其重命名為offset)以在第一頁之后的任何分頁鏈接上寫入兩次。 原因是當我重新創建查詢字符串偏移量時,該偏移量已經在后續頁面的$ _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);

使用分頁庫的CodeIgniter 3.0版本。 它有一個配置選項可重用查詢字符串。

我已經在CodeIgniter 2中實現了它,但沒有取代分布式版本,而是將其部署為名為MY_Pagination的重載庫並將其放置在“應用程序/庫”文件夾中。 為了使這種方式生效,我要做的唯一代碼更改就是將訪問修飾符設置為public而不是protected。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM