【转】yii 分页功能详解
Yii 是利用CPagination来实现
//在 Controller(控制器) 中的代码
public function actionIndex()
{
$criteria = new CDbCriteria();
$criteria->order = ' 数据库字段 desc'; //按什么字段来排序
$count = Article::model()->count('$criteria');//count() 函数计算数组中的单元数目或对象中的属性个数。
$pager = new CPagination('$count');
$pager -> pageSize = 10; //每页显示的行数
$pager->applyLimit($criteria);
$artList = Article::model()->findA($criteria);//查询所有的数据
$this->render('index',array('pages'=>$pager,'list'=>$artList));
}
下面是view中的代码
<div>
<?php
$this->widget('CLinkPager',array(
'header'=>'',
'fristPageLabel'=>'首页',
'lastPageLabel'=>'末页',
'prevPageLabel'=>'上一页',
'nextPageLabel'=>'下一页',
'pages'=>'$pages',
'maxButtonCount'=>13,
)
);
</div>
一般写法:
$criteria = new CDbCriteria;
$count = DnOnline::model()->count($criteria);
$pages = new CPagination($count);
$pages->pageSize = 5;
$pages->applylimit($criteria);
$model = DnOnline::model()->findAll($criteria);
$criteria = new CDbCriteria;
$count = DnOnline::model()->count($criteria);
$pages = new CPagination($count);
$pages->pageSize = 5;
$pages->applylimit($criteria); $model = DnOnline::model()->findAll($criteria);
CDB写法:
$criteria = new CDbCriteria;
$sql = "SELECT * FROM USER";
$model= Yii::app()->db->createCommand($sql)->queryAll();
$pages = new CPagination(count($model));
$pages->pageSize = 4;
$pages->applylimit($criteria);
$model=Yii::app()->db->createCommand($sql." LIMIT :offset,:limit");
$model->bindValue(':offset', $pages->currentPage*$pages->pageSize);
$model->bindValue(':limit', $pages->pageSize);
$model=$model->queryAll();
CDB写法: $criteria = new CDbCriteria;
$sql = "SELECT * FROM USER";
$model= Yii::app()->db->createCommand($sql)->queryAll();
$pages = new CPagination(count($model)); $pages->pageSize = 4;
$pages->applylimit($criteria);
$model=Yii::app()->db->createCommand($sql." LIMIT :offset,:limit");
$model->bindValue(':offset', $pages->currentPage*$pages->pageSize); $model->bindValue(':limit', $pages->pageSize); $model=$model->queryAll();
$criteria=new CDbCriteria();
$result = Yii::app()->db->createCommand($sql_do)->query();
$pages=new CPagination($result->rowCount);
$pages->pageSize=2;
$pages->applyLimit($criteria);
$result=Yii::app()->db->createCommand($sql_do." LIMIT 2");
$result->bindValue(':offset', $pages->currentPage*$pages->pageSize);
$result->bindValue(':limit', $pages->pageSize);
$list=$result->query();