/*public function actionReport(){$sql = "select * from {{goods}}";$criteria=new CDbCriteria();$result">
当前位置:Gxlcms > PHP教程 > php分页类用法(摘自yii框架)

php分页类用法(摘自yii框架)

时间:2021-07-01 10:21:17 帮助过:10人阅读

  1. php;toolbar:false">/*public function actionReport()
  2. {
  3. $sql = "select * from {{goods}}";
  4. $criteria=new CDbCriteria();
  5. $result = Yii::app()->db->createCommand($sql)->query();
  6. $pages=new CPagination($result->rowCount);
  7. $pages->pageSize=2;
  8. $pages->applyLimit($criteria);
  9. $result=Yii::app()->db->createCommand($sql." LIMIT :offset,:limit");
  10. $result->bindValue(':offset', $pages->currentPage*$pages->pageSize);
  11. $result->bindValue(':limit', $pages->pageSize);
  12. $goodsInfo=$result->query();
  13. $this->render('index',array(
  14. 'goodsInfo'=>$goodsInfo,
  15. 'pages'=>$pages,
  16. ));
  17. }*/
  1. foreach($goodsInfo as $v){
  2. ?>
  3. }
  4. ?>
  5. //分页widget代码:
  6. $this->widget('CLinkPager',array('pages'=>$pages));
  7. ?>
  8. 商品name价格更新时间

方式二,使用自己的分页类来完成分页 1,控制器代码:

  1. public function actionIndex(){

  2. $goods_m=goods::model();

  3. //1.获得商品总的记录数目

  4. $cnt = $goods_m -> count();

  5. //2. 实例化分页类对象

  6. $page = new Page($cnt, 2);

  7. //3. 重新按照分页的样式拼装sql语句进行查询

  8. $sql = "select * from {{goods}} $page->limit";
  9. $goodsInfo = $goods_m -> findAllBySql($sql);

  10. //4. 获得分页页面列表(需要传递到视图模板里边显示)

  11. $pageList = $page->fpage();

  12. $this->renderPartial('index',array('goodsInfo'=>$goodsInfo,'pageList'=>$pageList));

  13. }

2,视图代码:

  1. foreach($goodsInfo as $v){
  2. ?>
  3. }
  4. ?>
  5. echo $pageList;
  6. ?>
  7. 商品namepricecreate_time

分页类要放到能自动加载到的目录下就行;

人气教程排行