yii, pagination simple

Pagination in yii is quite simple. Before you can understand the solution i am presenting below you need to know how YII works basically and the model controller and view approach of yii:

Add following code in your controller action function:

function actionIndex(){
$criteria = new CDbCriteria();
$count=Article::model()->count($criteria);
$pages=new CPagination($count);

// results per page
$pages->pageSize=10;
$pages->applyLimit($criteria);
$models = Post::model()->findAll($criteria);

$this->render('index', array(
'models' => $models,
'pages' => $pages
));
}


And to display the pagination in view file simply use following code:

<?php foreach($models as $model): ?>
// display a model
<?php endforeach; ?>

// display pagination
<?php $this->widget('CLinkPager', array(
'pages' => $pages,
)) ?>

4 comments

Leave a comment

Your email address will not be published. Required fields are marked *