Pagination example in Php Yii Framework

Pagination example in Php Yii Framework

While working we need to perform pagination.  Whether to show the list of articles, users or products, we need pagination. In Yii it is very easy to implement pagination.

function actionSearch(){
    $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
    ));
}

 

The above code will get 10 results per page from Database.

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

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

You can also find more detail on Yii documentation on their website.