Yii framework分頁的一種方法
Controller裡的方法
public function actionList()
{
$sql='select count(*) from {{table}}';
$connection=Yii::app()->db;
//獲取總行數
$count=$connection->createCommand($sql)->queryScalar();
$criteria=new CDbCriteria;
$pages=new CPagination($count);
$pages->pageSize=10;
$pages->applylimit($criteria);
$sql='select * from {{table}}';
$List=$connection->createCommand($sql." LIMIT :Offset,:Limit")
->bindValue(':Offset', $pages->currentPage*$pages->pageSize)
->bindValue(':Limit', $pages->pageSize)
->queryAll();
$this->render('list',array(
'List'=>$List,
'pages'=>$pages,
));
}
views裡加入
<?php
$this->widget('CLinkPager',array(
'header'=>'',
'firstPageLabel' => '<<',
'lastPageLabel' => '>>',
'prevPageLabel' => '上一頁',
'nextPageLabel' => '下一頁',
'pages' => $pages,
'maxButtonCount'=>9,
'cssFile'=>'',
));
?>
之後自己再編寫樣式就可以了