Yii分页的写法

AR写法:
$CDbCriteria = new CDbCriteria();
$CDbCriteria->order = ‘`id` desc’;
$count=MessageModel::model()->count($CDbCriteria);
$pages=new CPagination($count);

$pages->pageSize=8;
$pages->applyLimit($CDbCriteria);
$model = MessageModel::model()->findAll($CDbCriteria);
CDB写法:
$sql=’select * from tableName’;
$criteria=new CDbCriteria();
$result = Yii::app()->db->createCommand($sql)->query();
$count = $result->rowCount;
$pages=new CPagination($count);
$pages->pageSize=$this->pageSize;
$pages->applyLimit($criteria);
$result=Yii::app()->db->createCommand($sql.” LIMIT  :offset,:limit”);
$result->bindValue(‘:offset’, $pages->currentPage*$pages->pageSize);
$result->bindValue(‘:limit’, $pages->pageSize);
$model=$result->queryAll();
//$pages分页 $model数据

视图代码 

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

  2. <?php echo CHtml::link($model->title,array(‘article/view’,'id’=>$model->id));?>

  3. <?php endforeach;?>

  4. 分页widget代码:

  5. <?php $this->widget(‘CLinkPager’,array(‘pages’=>$pages));?>


你可能感兴趣的:(Yii分页的写法)