?
分類:?php?學習筆記2013-10-28 16:13?992人閱讀?評論(2)?收藏?舉報
ci分頁搜索ci條件搜索分頁ci編寫條件分頁搜索ci編寫條件搜索codeigniter條件搜索
一动看、背景說明
由于公司新啟動了一個項目,在開發(fā)的前期,老大說這次用CI框架來做布持,于是就開始了各種研究筐眷。因為是管理系統(tǒng)的項目肿孵,所以會有很多的搜索分頁實現(xiàn)澳眷,但是在用CI原生的分頁時候發(fā)覺達不到效果胡嘿。比如說,有些時候需要同時保存username和gender的條件查詢而進行分頁境蔼,試了用session和get的方法灶平,最后折騰了一天,終于搞定了箍土,現(xiàn)在記錄一下這個過程吧逢享。
二、實現(xiàn)說明
利用URI類的segment來判斷是否為第一次加載吴藻,以致設(shè)置相應(yīng)的查詢條件瞒爬;
利用session來保存相應(yīng)的查詢條件;
設(shè)置分頁類來達到效果沟堡;
三侧但、具體步驟
1.設(shè)置公共函數(shù),進行分頁設(shè)置選項航罗,具體代碼如下:
[php]?view plaincopy
/**?
?????*?分頁生成?
?????*?@param?string?$base_url??當前分頁URL?
?????*?@param?int?$total_rows???數(shù)據(jù)總條數(shù)?
?????*?@param?int?$per_page?????每頁顯示數(shù)據(jù)條數(shù)?
?????*?@param?int?$uri_segment??分頁方法自動測定你?URI?的哪個部分包含頁數(shù)?
?????*?@param?int?$cur_page?????當前頁碼禀横,用于條件查詢時初始返回第一頁?
?????*?@return?mixed????????????分頁信息?
?????*/??
????public?function?getPageConfigInfo($base_url?=?null,?$total_rows?=?0,?$per_page?=?0,?$uri_segment?=?0,$offset=0)?{??
????????$config?=?array();??
????????if?($offset?==?0)?{??
????????????$cur_page?=?1;??
????????}??
????????if(isset($cur_page)){??
????????????$config['cur_page']?=?$cur_page;??
????????}??
????????$config['base_url']?=?site_url()?.?$base_url;??
????????$config['total_rows']?=?$total_rows;??
????????$config['per_page']?=?$per_page;??
????????$config['uri_segment']?=?$uri_segment;??
????????$config['full_tag_open']?=?"<table?width='100%'>?<tr??style=\"background-color:#eee;line-height:25px;\"?align='right'><td>";??
????????$config['full_tag_close']?=?'</td></tr></table>';??
????????$config['first_link']?=?'首頁';??
????????$config['first_tag_open']?=?'?<span?class="page_link">[';??
????????$config['first_tag_close']?=?']</span>?';??
????????$config['last_link']?=?'尾頁';??
????????$config['last_tag_open']?=?'?<span?class="page_link">[';??
????????$config['last_tag_close']?=?']</span">?';??
????????$config['next_link']?=?'下一頁';??
????????$config['next_tag_open']?=?'?<span?class="page_link">[';??
????????$config['next_tag_close']?=?']</span">?';??
????????$config['prev_link']?=?'上一頁';??
????????$config['prev_tag_open']?=?'<span?class="page_link">[';??
????????$config['prev_tag_close']?=?']</span">';??
????????$config['num_tag_open']?=?'?<span?class="page_link">[';??
????????$config['num_tag_close']?=?']</span">?';??
????????$config['cur_tag_open']?=?'?[<span?style="color:red;font-weight:bold;">';??
????????$config['cur_tag_close']?=?'</span>]?';??
????????return?$config;??
????}??
大概說一下,除了基本CI要求的參數(shù)之外粥血,還有一個參數(shù):
[php]?view plaincopy
$config['cur_page']?=?$cur_page;??
????????這個是設(shè)置當前頁碼的參數(shù)柏锄,多設(shè)置這個參數(shù)有一個原因,當用戶點擊查詢重新加載新的數(shù)據(jù)的時候复亏,需要分頁顯示的頁碼設(shè)置成第一頁趾娃,所以用了如下代碼來判斷:
[php]?view plaincopy
????if?($offset?==?0)?{??
????????$cur_page?=?1;??
????}??
??????即偏移量為0則跳轉(zhuǎn)到第一頁;
2.在Controllers設(shè)置相應(yīng)處理
部分代碼如下:
[php]?view plaincopy
????//計算偏移量缔御,保留和刪除session條件??
????$offset?=?0;??
????if?($this->uri->segment($this->uri_segment)?!=?'')?{??
????????$offset?=?(int)?($this->uri->segment($this->uri_segment)?-?1)?*?$this->per_page;??
????????if?($this->session->userdata('search')?!=?'')?{??
????????????$where?=?$this->session->userdata('search');?//保持條件查詢??
????????}??
????}?else?{??
????????$this->session->unset_userdata('client_ver');??
????????$this->session->unset_userdata('deal_state');??
????????$this->session->unset_userdata('phone_type');??
????????$this->session->unset_userdata('begin_dt');??
????????$this->session->unset_userdata('end_dt');??
????????$this->session->unset_userdata('search');??
????}??
????//客戶端版本條件查詢??
????if?($this->input->post('client_version')?!=?'')?{??
????????$_client_ver?=?$this->input->post('client_ver');??
????????$where?.=?"?AND?client_version?LIKE?'%"?.?$_client_ver?.?"%'";??
????????$this->session->set_userdata('client_ver',?$_client_ver);??
????}?else?{??
????????$this->session->unset_userdata('client_ver');??
????}??
????//操作狀態(tài)條件查詢??
????if?($this->input->post('deal_state')?!=?'')?{??
????????$_deal_state?=?$this->input->post('state');??
????????$where?.=?"?AND?deal_state='"?.?$_deal_state?.?"'";??
????????$this->session->set_userdata('deal_state',?$_deal_state);??
????}?else?{??
????????$this->session->unset_userdata('deal_state');??
????}??
????? 至此抬闷,獲取前臺傳入數(shù)據(jù)已經(jīng)基本結(jié)束
3.修改Pagination.php類
???????1)先$use_page_numbers將此變量設(shè)置為TRUE,用頁碼代替偏移量耕突,即顯示分頁時候1笤成,2,3...
???????2)來到create_links()方法有勾,對如下代碼:
[php]?view plaincopy
????if?($CI->uri->segment($this->uri_segment)?!=?$base_page)??
????{??
????????$this->cur_page?=?$CI->uri->segment($this->uri_segment);??
????????//?Prep?the?current?page?-?no?funny?business!??
????????$this->cur_page?=?(int)?$this->cur_page;??
????}??
if語句進行修改為:
[php]?view plaincopy
if?(($CI->uri->segment($this->uri_segment)?!=?$base_page)&&?$this->cur_page?==?0)??
用這個判斷來修復(fù)當用戶點擊查詢時疹启,實際為第一頁,但是頁碼顯示第二頁的BUG蔼卡;
???? 3)最后一個地方的修改喊崖,
[php]?view plaincopy
$first_url?=?($this->first_url?==?'')???$this->base_url?:?$this->first_url;??
這條語句是用來首頁的代碼,因為本方法是通過判斷segment來判斷是否為首次加載雇逞,原生CI分頁當點擊首頁時出現(xiàn)的鏈接地址此部分segment為空荤懂,就會導(dǎo)致當已經(jīng)選擇條件查詢分頁時,點擊首頁把所有條件刪除塘砸,所以改為如下即可:
[php]?view plaincopy
$first_url?=?($this->first_url?==?'')???$this->base_url.'1'?:?$this->first_url;??
四节仿、總結(jié)
到現(xiàn)在已經(jīng)完成了CI的分頁搜索,因為接觸時間也不長掉蔬,有錯誤之處歡迎指正:-)
版權(quán)聲明:本文為博主原創(chuàng)文章廊宪,未經(jīng)博主允許不得轉(zhuǎn)載矾瘾。
來源:?<http://blog.csdn.net/zhaoyu7777777/article/details/13295249>