Yii 分页

kevin.Zhu 发布于:2017-5-28 17:30 分类:Php  有 23 人浏览,获得评论 0 条  

http://www.cnblogs.com/xshang/p/3887045.html



 yii中使用分页很方便,如下两种方法:

      在控制器中:

      1、

1
2
3
4
5
6
7
$criteria new CDbCriteria();     //new cdbcriteria数据库<br>$criteria->id = 'id ASC';           //排序规则
$count = Exchange::model()->count($criteria);
$pager new CPagination($count);
$pager->pageSize=30;
$pager->applyLimit($criteria);
         
$categoryInfo = Category::model()->findAll($criteria); //根据条件查询

  2、

1
2
3
4
5
6
7
8
9
$criteria new CDbCriteria();
$criteria->order = 'id ASC';
$criteria->addCondition('status=1');      //根据条件查询
$criteria->addCondition('exchange_status=0');
$count = Exchange::model()->count($criteria);
$pager new CPagination($count);
$pager->pageSize=30;
$pager->applyLimit($criteria); 
$exchangeInfo = Exchange::model()->findAll($criteria);

 render中传入参数:

   array("pages" => $pager)

 视图中加入:

 

1
2
3
4
5
6
7
8
9
10
$this->widget('CLinkPager',array(
               'header'=>'',
               'firstPageLabel' => '首页',
               'lastPageLabel' => '末页',
               'prevPageLabel' => '上一页',
               'nextPageLabel' => '下一页',
               'pages' => $pages,
               'maxButtonCount'=>8,
         )
         );

  分页思想: 1、计算数据库中总的条数

                    2、分页大小

                    3、设置偏移量limit

    在Yii中,分页时会用这个类CDBcritria进行数据库查询很重要,这样分页很简单。