Yii框架分頁實現(xiàn)方法詳解
本文實例講述了Yii框架分頁實現(xiàn)方法。分享給大家供大家參考,具體如下:
下家公司用的框架是yii,這幾天看了下相關教程,今兒把分頁教程寫下,最后把tp的分頁也給整合進了yii,因為個人覺得yii分頁沒有tp用的順手。
首頁,在models目錄里創(chuàng)建個Auth.php的模型文件,里面寫入
class Auth extends CActiveRecord { public static function model($className = __CLASS__) { return parent::model($className); } public function tableName() { return '{{auth}}'; } }
接著在controllers目錄里創(chuàng)建IndexController.php的控制文件,里面寫入
class IndexController extends Controller { public function actionList() { $criteria = new CDbCriteria(); $criteria->order = 'a_id desc'; $count = Auth::model()->count($criteria); $page = new CPagination($count); $page->pageSize = 10; $page->applyLimit($criteria); $auth = Auth::model()->findAll($criteria); $this->renderPartial('auth', array('page' => $page, 'list' => $auth)); } public function actionList1() { $p = isset($_GET['page']) ? $_GET['page'] : 0; $criteria = new CDbCriteria(); $criteria->select = "a_id,a_nickname"; $criteria->condition=''; $criteria->limit = 10; $criteria->offset = $p == 0 ? 0 : (($p-1)*10); $criteria->order = 'a_id desc'; $auth = Auth::model()->findAll($criteria); $count = Auth::model()->count($criteria); $page = new CPagination($count); $page->pageSize = 10; $page->applyLimit($criteria); $this->renderPartial('auth', array('page' => $page, 'list' => $auth)); } }
其中actionList和actionList1是$criteria的兩種寫法
最后在views目錄里添加index目錄,并在index目錄內(nèi)添加auth.php文件,里面寫入
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <div class="blogList"> <ul> <?php foreach($list as $key=>$value){ ?> <li> <a><?php echo $value['a_nickname'];?></a> </li> <?php } ?> </ul> </div> <div id="page"> <?php $this->widget('CLinkPager',array( 'firstPageLabel'=>'首頁', 'lastPageLabel'=>'末頁', 'prevPageLabel'=>'上一頁', 'nextPageLabel'=>'下一頁', 'pages'=>$page, 'maxButtonCount'=>13, ) ); ?> </div>
上面是yii自帶的寫法,這里引入tp的分頁類,做個簡單的改動,步驟如下
首先,把tp的AjaxPage.class.php和Page.class.php移動到y(tǒng)ii的項目目錄下的 protected/components下面,并且把文件名稱分布改為AjaxPage.php和Page.php,分別進入兩個文件,把里面的C方法去掉,也就是這一句
$this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
改為
$this->varPage = 'p' ;
改完之后,這個兩個文件是不需要引入的,因為yii在啟動時會自動加載的。具體的可見protected/config.php/main.php的配置中的
// autoloading model and component classes 'import'=>array( 'application.models.*', 'application.components.*', ),
其次,在protected/config.php/目錄里新建一個common.php文件,這個文件就放些項目的公共函數(shù),熟悉tp的朋友應該知道tp也有公共函數(shù)文件,很好用,這里借鑒下,yii應該也有吧,目前還沒發(fā)現(xiàn)。在該文件寫入
// 根據(jù)頁碼獲取列表 function getListByPage($model, $select = '*', $condition = '', $limit = 10, $order = '', $p = '', $ajax = 0) { // 初始化參數(shù) $_GET['p'] = isset($_GET['p']) ? intval($_GET['p']) : 1; $limit = intval($limit) > 0 ? intval($limit) : 10; if ($p) { $_GET['p'] = intval($p) ? intval($p) : 1; } $criteria = new CDbCriteria(); $count = $model->count($criteria); if ($ajax) { $Page = new AjaxPage($count, $limit); } else { $Page = new Page($count, $limit); } $result['page'] = trim($Page->show()); $criteria->select = $select; $criteria->condition = $condition; $criteria->limit = $Page->listRows; $criteria->offset = $Page->firstRow; $criteria->order = $order; $list = $model->findAll($criteria); $result['list'] = $list; return $result; }
這個文件可就要引入了,不然加載不了,可以在項目的入口文件index.php里自行引入,代碼如下
require_once(dirname($config) . '/common.php');
最后在indexController.php中用到分頁的地方調(diào)用該方法
public function actionPage() { $model = Auth::model(); $info = getListByPage($model); $this->renderPartial('page', array('info' => $info)); }
封裝了此方法,以后調(diào)用分頁時,只需傳幾個參數(shù),簡單又快捷。在page.php頁面上調(diào)用
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <div class="blogList"> <ul> <?php foreach($info['list'] as $key=>$value){ ?> <li> <a><?php echo $value['a_nickname'];?></a> </li> <?php } ?> </ul> </div> <div id="page"> <?php echo $info['page']; ?> </div>
同時利用findAll也可以實現(xiàn)分頁的查詢效果,代碼如下
function getListByPage($model, $select = '*', $condition = '', $limit = 10, $order = '', $p = '', $ajax = 0) { if (!$model) { return array();; } // 初始化參數(shù) $_GET['p'] = isset($_GET['p']) ? intval($_GET['p']) : 1; $limit = intval($limit) > 0 ? intval($limit) : 10; if ($p) { $_GET['p'] = intval($p) ? intval($p) : 1; } $count = $model->count(); if ($ajax) { $Page = new AjaxPage($count, $limit); } else { $Page = new Page($count, $limit); } $result['page'] = trim($Page->show()); $result['list'] = $model->findAll(array( 'select' => $select, 'condition' => $condition, 'order' => $order, 'limit' => $Page->listRows, 'offset' => $Page->firstRow, )); return $result; }
總結:
經(jīng)歷過ci、tp兩個框架后,再看yii進度快很多。掌握某個框架,個人認為無非就是掌握mvc的使用規(guī)則,在model層調(diào)用數(shù)據(jù)庫方法得到數(shù)據(jù),controller層調(diào)用model層數(shù)據(jù)并進行邏輯處理,再傳給view層,同時了解框架的模板操作,表單操作,分頁操作,文件上傳操作,cookie和session操作,url調(diào)用,這些掌握了,在經(jīng)過項目的磨合,就差不多了,理解了常用操作之后,再去看看源碼,對比并總結框架間的區(qū)別和共性,從而升華自己的技術,以后常用開發(fā)就不在話下,拿可觀的薪水也是如此。
更多關于Yii相關內(nèi)容感興趣的讀者可查看本站專題:《Yii框架入門及常用技巧總結》、《php優(yōu)秀開發(fā)框架總結》、《smarty模板入門基礎教程》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Yii框架的PHP程序設計有所幫助。
相關文章
讓CodeIgniter數(shù)據(jù)庫緩存自動過期的處理的方法
按官方的說法,緩存設置后永不過期,除非你調(diào)用方法主動刪除。這篇文章主要介紹了CodeIgniter數(shù)據(jù)庫緩存自動過期的處理,需要的朋友可以參考下2014-06-06laravel-admin 管理平臺獲取當前登陸用戶信息的例子
今天小編就為大家分享一篇laravel-admin 管理平臺獲取當前登陸用戶信息的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10利用PHP訪問數(shù)據(jù)庫_實現(xiàn)分頁功能與多條件查詢功能的示例
下面小編就為大家?guī)硪黄肞HP訪問數(shù)據(jù)庫_實現(xiàn)分頁功能與多條件查詢功能的示例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09php5 apache 2.2 webservice 創(chuàng)建與配置(java)
要運行wsCaller.jar 要選安裝jdk 如果沒有安裝jdk 則wsCaller.jar 會以壓縮包的形式顯示2011-01-01