最近在用CI框架的時候,用了CI的分頁類,以前是用前端整分頁,這次干脆用用框架自帶的,自己這個健忘的腦袋,還是記錄一下吧。
因為頁面中有條件篩選的表單,所以想要完成的效果就是,輸入條件后,分頁跳轉之后能維持所輸入的條件。想了一下,自己的思路如下代碼吧。
controller 代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class Monitors extends CI_Controller { public function warning(){ $config = array (); $config [ 'per_page' ] = 15; //每頁顯示的數據數 $current_page = intval ( $this ->input->get( "per_page" )); //獲取當前分頁頁碼數 $status = $this ->input->get( "filter-status" ,TRUE); $level = $this ->input->get( 'filter-level' ,TRUE); $timestamp = $this ->input->get( 'filter-timestamp' ,TRUE); $all = $this ->monitors_m->getAllData( $current_page , $config [ 'per_page' ], $status , $timestamp , $level ); //這里返回的有總條數和具體的數據,根據自己的情況略加修改即可 $data [ 'allevent' ] = $all [ 'content' ]; $config [ 'total_rows' ] = $all [ 'count' ]; //總條數 $config [ 'num_links' ] = 3; //頁碼連接數 $config [ 'use_page_numbers' ] = TRUE; $config [ 'page_query_strings' ] = TRUE; //關鍵配置 $config [ 'base_url' ] = base_url(). 'index.php/monitors/warning?' &filter-status= '.$status.' &filter-level= '.$level.' &filter-timestamp='. $timestamp ; //關鍵配置 $this ->load->library( 'pagination' ); //加載ci pagination類 $this ->pagination->initialize( $config ); $data [ 'page' ] = $this ->pagination->create_links(); //關鍵代碼 $this ->load->view( "monitors_v" , $data ); } |
關鍵配置參數
1
|
$config [‘page_query_string'] |
如果設置成true,則url則是”index.php/monitors/warning?per_page=20”這樣的
【注】”per_page” 是默認傳遞的查詢字符串,但也可以使用 $config[‘query_string_segment'] = ‘你的字符串' 來配置
在我的方案中,設置為TRUE,當然TRUE是默認值,不管也可以;
1
|
$config [‘base_url'] |
一開始只是設置為以下這種情況的時候,在某一頁進行條件篩選是可以的,但是跳轉后由于刷新的問題條件又沒有了。
1
|
$config [ 'base_url' ] = base_url().'index.php/monitors/warning; |
采用以下的方式即可,吼吼吼
1
2
3
4
|
$status = $this ->input->get( "filter-status" ,TRUE); $level = $this ->input->get( 'filter-level' ,TRUE); $timestamp = $this ->input->get( 'filter-timestamp' ,TRUE); $config [ 'base_url' ] = base_url(). 'index.php/monitors/warning?' &filter-status= '.$status.' &filter-level= '.$level.' &filter-timestamp='. $timestamp ; //關鍵配置 |
view頁面代碼
就一句話,在你需要放置分頁元素的地方加上這樣一句就行,這里的$page變量就是在controller里存進去的$this->pagination->create_links();
1
|
<?php echo $page ?> |
設置分頁樣式
這里采用的是bootstrap的樣式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
$config [ 'first_link' ] = "<<" ; //首頁 $config [ 'prev_link' ] = "<" ; //上一頁 $config [ 'next_link' ] = ">" ; //下一頁 $config [ 'last_link' ] = ">>" ; //尾頁 $config [ 'full_tag_open' ] = '<ul class="pagination pagination-split">' ; $config [ 'full_tag_close' ] = '</ul>' ; $config [ 'first_tag_open' ] = '<li>' ; //第一個鏈接的起始標簽。 $config [ 'first_tag_close' ] = '</li>' ; //第一個鏈接的結束標簽。 $config [ 'next_tag_open' ] = '<li>' ; //下一頁鏈接的起始標簽。 $config [ 'next_tag_close' ] = '</li>' ; //下一頁鏈接的結束標簽。 $config [ 'prev_tag_open' ] = '<li>' ; //上一頁鏈接的起始標簽。 $config [ 'prev_tag_close' ] = '</li>' ; //上一頁鏈接的結束標簽。 $config [ 'cur_tag_open' ] = '<li class="active"><a>' ; $config [ 'cur_tag_close' ] = '</a></li>' ; //當前頁鏈接的結束標簽。 $config [ 'num_tag_open' ] = '<li>' ; //數字鏈接的起始標簽。 $config [ 'num_tag_close' ] = '</li>' ; //數字鏈接的結束標簽。 |
以上所述是小編給大家介紹的PHP CodeIgniter分頁實例及多條件查詢解決方案,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的,在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.tuicool.com/articles/7b2iqqy