Yii使用CPagination分页

 

 

[html]  view plain copy
  1. public function actionIndex()    
  2.     {    
  3.         $criteria = new CDbCriteria();    
  4.         $criteria->order = 'createtime desc';        
  5.         $count = Article::model()->count($criteria);    
  6.             
  7.         $pager = new CPagination($count);    
  8.         $pager->pageSize = 10;             
  9.         $pager->applyLimit($criteria);    
  10.     
  11.         $artList = Article::model()->findAll($criteria);    
  12.         $this->render('index',array('pages'=>$pager,'list'=>$artList));    
  13.     }    

 

在视图中代码如下:

[html]  view plain copy
  1. <div id="pager">    
  2.     <?php    
  3.     
  4.     $this->widget('CLinkPager',array(    
  5.         'header'=>'',    
  6.         'firstPageLabel' => '首页',    
  7.         'lastPageLabel' => '末页',    
  8.         'prevPageLabel' => '上一页',    
  9.         'nextPageLabel' => '下一页',    
  10.         'pages' => $pages,    
  11.         'maxButtonCount'=>13    
  12.         )    
  13.     );    
  14.     ?>    
  15.     </div>    

ps:一般写法:

 

[html]  view plain copy
  1. $criteria = new CDbCriteria;    
  2. $count = DnOnline::model()->count($criteria);    
  3.     
  4. $pages = new CPagination($count);                   
  5. $pages->pageSize = 5;    
  6. $pages->applylimit($criteria);    
  7.     
  8. $model = DnOnline::model()->findAll($criteria);  

[html]  view plain copy
  1. CDB写法:    
  2.     
  3.     $criteria = new CDbCriteria;    
  4.     $sql = "SELECT * FROM USER";    
  5.     $modelYii::app()->db->createCommand($sql)->queryAll();    
  6.     $pages = new CPagination(count($model));                   
  7.     $pages->pageSize = 4;    
  8.     $pages->applylimit($criteria);    
  9.     
  10.     $model=Yii::app()->db->createCommand($sql." LIMIT :offset,:limit");    
  11.     $model->bindValue(':offset', $pages->currentPage*$pages->pageSize);    
  12.     $model->bindValue(':limit', $pages->pageSize);    
  13.     $model=$model->queryAll();   
[html]  view plain copy
  1. $criteria=new CDbCriteria();    
  2.             $result = Yii::app()->db->createCommand($sql_do)->query();    
  3.            $pages=new CPagination($result->rowCount);    
  4.           $pages->pageSize=2;    
  5.             $pages->applyLimit($criteria);    
  6.           $result=Yii::app()->db->createCommand($sql_do." LIMIT 2");    
  7.            $result->bindValue(':offset', $pages->currentPage*$pages->pageSize);    
  8.          $result->bindValue(':limit', $pages->pageSize);    
  9.          $list=$result->query();   

在yii中有自带的分页方式,通过cgridview等

mongo扩展使用统一方法

   
   
   
   
  1. /** 
  2.      * 分页查询 
  3.      * @param DBModel           $model      数据所在表的模型对象 
  4.      * @param EMongoCriteria    $criteria   查询过滤器 
  5.      * @param int               $size       每页记录数,默认10 
  6.      * @return 包含查询结果[rs]和分页信息[pages]的数组 
  7.      */ 
  8.     public function makePage($model$criteria=array(), $size=10){ 
  9.         $count = $model->count($criteria);     
  10.          
  11.         //创建分页数据并加入到查询器中 
  12.         $pager = new CPagination($count); 
  13.         $pager->pageSize = $size
  14.         $criteria->offset($pager->getCurrentPage()*$pager->getPageSize());  
  15. //无法使用$pager->applyLimit($criteria);代替 
  16.         $criteria->limit($pager->getPageSize()); 
  17.          
  18.         //获取用户数据 
  19.         $rs = $model->findAll($criteria);  
  20.         return array('rs'=>$rs,'pages'=>$pager); 
  21.     } 

 

 

你可能感兴趣的:(分页,yii)