- public function actionIndex()
- {
- $criteria = new CDbCriteria();
- $criteria->order = 'createtime desc';
- $count = Article::model()->count($criteria);
- $pager = new CPagination($count);
- $pager->pageSize = 10;
- $pager->applyLimit($criteria);
- $artList = Article::model()->findAll($criteria);
- $this->render('index',array('pages'=>$pager,'list'=>$artList));
- }
在视图中代码如下:
- <div id="pager">
- <?php
- $this->widget('CLinkPager',array(
- 'header'=>'',
- 'firstPageLabel' => '首页',
- 'lastPageLabel' => '末页',
- 'prevPageLabel' => '上一页',
- 'nextPageLabel' => '下一页',
- 'pages' => $pages,
- 'maxButtonCount'=>13
- )
- );
- ?>
- </div>
ps:一般写法:
- $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();
- $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();
在yii中有自带的分页方式,通过cgridview等
mongo扩展使用统一方法
- /**
- * 分页查询
- * @param DBModel $model 数据所在表的模型对象
- * @param EMongoCriteria $criteria 查询过滤器
- * @param int $size 每页记录数,默认10
- * @return 包含查询结果[rs]和分页信息[pages]的数组
- */
- public function makePage($model, $criteria=array(), $size=10){
- $count = $model->count($criteria);
- //创建分页数据并加入到查询器中
- $pager = new CPagination($count);
- $pager->pageSize = $size;
- $criteria->offset($pager->getCurrentPage()*$pager->getPageSize());
- //无法使用$pager->applyLimit($criteria);代替
- $criteria->limit($pager->getPageSize());
- //获取用户数据
- $rs = $model->findAll($criteria);
- return array('rs'=>$rs,'pages'=>$pager);
- }