Latest news:

New android application Islamic Video has been released. its a free islamic video application for android smartphone. Download now!

Yii2 pagination

Pagination represents information relevant to pagination of data items.

When data needs to be rendered in multiple pages, Pagination can be used to represent information such as total item count, page size, current page, etc. These information can be passed to pagers to render pagination buttons or links.

The following example shows how to create a pagination object and feed it to a pager.

Controller action:

public function actionSearch($q = '') {
        $this->layout = 'site_main';

        $q = trim($q);

        $sql = "SELECT * FROM
(select p.product_id as 'sid',p.name_en,p.name_ar,p.description_en,description_ar,p.image, 'product' as 'type', 'none' as 'category_type'
from tbl_products p
select sl.service_location_id as 'sid' ,sl.name_en,sl.name_ar,sl.description_en,sl.description_ar,sl.image,'servicelocation' as 'type' ,'none' as 'category_type'
from tbl_service_locations sl
as search
WHERE search.name_en LIKE '$q%' or search.name_ar LIKE '$q%' or search.description_en LIKE '$q%' or search.description_ar LIKE '$q%'  
        $query = Yii::$app->db->createCommand($sql)->queryAll();

        $count = sizeof($query);

        //echo $count;

        $dataProvider = new SqlDataProvider([
            'sql' => $sql,
            'totalCount' => $count,
            'pagination' => [
                'pageSize' => 10,

        //$models = $dataProvider->query;

        return $this->render('search', [
                    'q' => $q,
                    'result' => $dataProvider->getModels(),
                    'dataProvider' => $dataProvider,
                    'count' => $count,

View :

foreach ($result as $row) {

 // display $model here


// display pagination

echo yiiwidgetsLinkPager::widget([
    'pagination' => $dataProvider->pagination,

For reference please visit



Tags : PHP, Yii2,

Views : 3

Recent Posts


Popular Tags


Subscribe Us

Follow Us