控制器代码如下:

  1. $sql = "SELECT * FROM table WHERE cid=2 and status=1";

  2. $criteria=new CDbCriteria();

  3. $result = Yii::app()->db->createCommand($sql)->query();

  4. $pages=new CPagination($result->rowCount);

  5. $pages->pageSize=22;

  6. $pages->applyLimit($criteria);

  7. $result=Yii::app()->db->createCommand($sql." LIMIT :offset,:limit");

  8. $result->bindValue(':offset', $pages->currentPage*$pages->pageSize);

  9. $result->bindValue(':limit', $pages->pageSize);

  10. $posts=$result->query();

  11. $this->render('index',array(

  12.         'posts'=>$posts,

  13.         'pages'=>$pages,

  14. ));

视图代码如下:

  1. 循环输出

  2. <?php foreach($posts as $model):?>

  3. <?php echo CHtml::link($model['title'],array('article/view','id'=>$model['id']));?>

  4. <?php endforeach;?>

  5. 分页widget代码:

  6. <?php $this->widget('CLinkPager',array('pages'=>$pages));?>