《PHP應(yīng)用:Codeigniter(CI)框架分頁函數(shù)及相關(guān)知識》要點(diǎn):
本文介紹了PHP應(yīng)用:Codeigniter(CI)框架分頁函數(shù)及相關(guān)知識,希望對您有用。如果有疑問,可以聯(lián)系我們。
PHP教程一般在數(shù)據(jù)分頁的時候必要獲取當(dāng)前頁的數(shù)據(jù)和總條數(shù),一般人是在model中封裝兩個函數(shù)分別獲取當(dāng)前頁的數(shù)據(jù)和數(shù)據(jù)總條數(shù),業(yè)務(wù)邏輯類似,感覺有點(diǎn)冗余,可以封裝在一起:
代碼如下:
/**
???? * 獲取分頁數(shù)據(jù)及總條數(shù)
???? * @param string @tablename 表名
???? * @param mixed $where 條件
???? * @param int $limit 每頁條數(shù)
???? * @param int $offset 當(dāng)前頁
???? */
??? public function get_page_data($tablename, $where, $limit, $offset, $order_by, $db)
??? {
??????? if(empty($tablename))
??????? {
??????????? return FALSE;
??????? }
???????
??????? $dbhandle = empty($db) ? $this->db : $db;
???????
??????? if($where)
??????? {
??????????? if(is_array($where))
??????????? {
??????????????? $dbhandle->where($where);
??????????? }
??????????? else
??????????? {
??????????????? $dbhandle->where($where, NULL, false);
??????????? }
??????? }
???????
??????? $db = clone($dbhandle);
??????? $total = $dbhandle->count_all_results($tablename);
???????
??????? if($limit)
??????? {
??????????? $db->limit($limit);
??????? }
???????
??????? if($offset)
??????? {
??????????? $db->offset($offset);
??????? }
???????
??????? if($order_by)
??????? {
??????????? $db->order_by($order_by);
??????? }
???????
??????? $data = $db->get($tablename)->result_array();
???????
??????? return array('total' => $total, 'data' => $data);
??? }
PHP教程CI框架分頁類使用心得
PHP教程CI分頁的url地址有四種方式
a) locahost/news/page/2 這個2表現(xiàn)第二頁
b) localhost/news/page/20 這個20表現(xiàn)從第20條記錄開始分頁,即頁面的第一條記錄,是數(shù)據(jù)庫中的第20條記錄.
c) localhost/news?per_page=2 第二頁
d) localhost/news?per_page=20 同b)
PHP教程首先我們先看一下CI分頁的參數(shù):
代碼如下:
$config['base_url'] = $url;??
/* 分頁的基礎(chǔ) URL
如果你想用a、b的鏈接形式,則該url應(yīng)該形式如/news/page/?
如果鏈接是c、d的形式,則url應(yīng)該如/news??
*/?
$config['total_rows'] = $total;//記錄總數(shù),這個沒什么好說的了,就是你從數(shù)據(jù)庫取得記錄總數(shù)??
$config['per_page'] = $pagesize; //每頁條數(shù).額,這個也沒什么好說的..本身設(shè)定.默認(rèn)為10好像.??
$config['page_query_string'] = TRUE;??
/*傳參形式.開啟true則會自動在你的url后面加上&per_page=3.(這個per_page是默認(rèn)的查詢字符,當(dāng)然你也可以用$config['query_string_segment']來本身設(shè)定)
因此c、d中的形式一般是為localhost/news?&per_page=2不過都一樣,沒什么影響.get的per_page還是3?
*/?
$config['first_link'] = '首頁'; // 第一頁顯示??
$config['last_link'] = '末頁'; // 最后一頁顯示??
$config['next_link'] = '下一頁 >'; // 下一頁顯示??
$config['prev_link'] = '< 上一頁'; // 上一頁顯示??
$config['cur_tag_open'] = ' <a class="current">'; // 當(dāng)前頁開始樣式??
$config['cur_tag_close'] = '</a>';??
/*當(dāng)前頁結(jié)束樣式.這些你可以本身嘗試一下.
比如說我想讓當(dāng)前頁的分頁數(shù)字樣式好看一點(diǎn),紅色字體等.你就可以在current上加上css代碼?
*/?
$config['num_links'] = 2;// 當(dāng)前連接前后顯示頁碼個數(shù).意思就是說你當(dāng)前頁是第5頁,那么你可以看到3、4、5、6、7頁.??
$config['uri_segment'] = 4;??
/*這個是你在用a)、b)鏈接樣式的時候,用來判斷頁頁數(shù).
比如localhost/news/page/3? 這個uri_segment就要設(shè)定為3.localhost/news/title/page/3這個就要設(shè)定為4?
*/?
$config['use_page_numbers'] = TRUE;??
/*這個就是a)、b)的差別了.開啟了,page就會表示頁數(shù).false就會表示記錄數(shù)
*/?
PHP教程剛開始在網(wǎng)上查資料的時候,有很多這種寫法.
代碼如下:
$this->model->get_news($config['per_page'],$this->uri->segment(3));?
PHP教程其實這種寫法便是針對b)這種連接形式的.這里的$this->uri->segment(3)便是取到page/20中的記錄數(shù)20.$config['per_page']便是限制輸出多少條.
有很大的局限性和誤導(dǎo)性.我開始便是死都不知道為什么這么寫..后來才發(fā)現(xiàn),手冊才是最好的老師.
PHP教程當(dāng)我們把CI分頁類的一些參數(shù)都配置好了之后,$this->pagination->initialize($config);//配置分頁
代碼如下:
$page = $this->pagination->create_links();? //我們就獲得了分頁了?
PHP教程直接傳遞到視圖頁,即可.
PHP教程至于怎么加載模型,怎么存取數(shù)據(jù)記錄,怎么傳遞變量到視圖,這里就不說了,看手冊好了.
PHP教程忘記說了,帶查詢參數(shù)的分頁,我是這么做的.視圖中將查詢參數(shù)get提交到控制器的search辦法.在search中,用$get = $this->input->get();去獲取到查詢參數(shù).
然后加載model,用帶查詢參數(shù)和分頁參數(shù)去讀取記錄,將結(jié)果顯示到視圖..
PHP教程另外還發(fā)現(xiàn)個小bug,好比/news/page/-1000這樣的時候,下面的分頁鏈接將會出現(xiàn)負(fù)值
發(fā)現(xiàn)system/libraries/Pagination.php代碼如下
代碼如下:
if ($this->use_page_numbers AND $this->cur_page == 0)??
{??
??? $this->cur_page = $base_page;??
}??
//應(yīng)為??
if ($this->use_page_numbers AND $this->cur_page <= 0)??
{??
??? $this->cur_page = $base_page;??
}?
PHP教程才對吧,顛末修改后,這個問題沒有了.
《PHP應(yīng)用:Codeigniter(CI)框架分頁函數(shù)及相關(guān)知識》是否對您有啟發(fā),歡迎查看更多與《PHP應(yīng)用:Codeigniter(CI)框架分頁函數(shù)及相關(guān)知識》相關(guān)教程,學(xué)精學(xué)透。維易PHP學(xué)院為您提供精彩教程。
轉(zhuǎn)載請注明本頁網(wǎng)址:
http://www.snjht.com/jiaocheng/14359.html