AR寫法:
$CDbCriteria = new CDbCriteria();
$CDbCriteria->order = ‘`id` desc’;
$count=MessageModel::model()->count($CDbCriteria);
$pages=new CPagination($count);php
$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數據sql
視圖代碼 app
<?php foreach($posts as $model):?>post
<?php echo CHtml::link($model->title,array(‘article/view’,'id’=>$model->id));?>this
<?php endforeach;?>spa
分頁widget代碼:orm
<?php $this->widget(‘CLinkPager’,array(‘pages’=>$pages));?>get