Create pagination in yii framework

2019-07-11 03:48发布

I want to create pagination on my page without grid-view or model. This is my controller code:-

        $providers = Yii::app()->db->createCommand($query)->queryAll();
        $count = Yii::app()->db->createCommand($query)->queryAll();

        $dataProvider = new CSqlDataProvider($query, array(
           'totalItemCount' => $count,
           'pagination' => array(
               'pageSize' => 10,
           ),
        ));

and my view code is:-

        <?php $this->widget('zii.widgets.grid.CGridView', array(
           'id'=>'students-grid',
           'dataProvider'=> $dataProvider, 
        )); ?>

But when I run this its show error message

    "Array to string conversion"

and 2nd option which I had try that is :-

        $criteria = new CDbCriteria();
        $count = Yii::app()->db->createCommand($query)->queryAll();
        $pages = new CPagination($count);

        // results per page
        $pages->pageSize=10;
        $pages->applyLimit($criteria);

        //$models=Article::model()->findAll($criteria);

But I don't know how to use this code because i have no model in this time.On the place of model what I can use?

3条回答
乱世女痞
2楼-- · 2019-07-11 04:28

Please Follow below URL. Here you can find how to make custom pagination. http://www.bsourcecode.com/yiiframework2/custom-pagination-in-yiiframework-2-0/

查看更多
虎瘦雄心在
3楼-- · 2019-07-11 04:46

Try this. Hope this help you.

//controller
$count = Yii::app()
   ->db //your count of records
   ->createCommand('
      select COUNT(*) AS total
        from providers p 
        left join providers_facilities_services fs on fs.provider_guid = p.provider_guid 
        left join lkup_facilitytypes l on l.lkup_facilitytype_id = fs.lkuptype_id and fs.type='F' left join providers_media m on m.provider_guid = p.provider_guid 
        left join providers_contacts as comp on comp.provider_guid= p.provider_guid where p.provider_class='F' and comp.contact_type='U' and fs.type='F' and p.deleted = 'N' and fs.lkuptype_id =5 HAVING distance <= 20000
   ')
   ->queryColumn();

$dataProvider = new CSqlDataProvider($query, array(
    'totalItemCount' => $count[0],
    'pagination' => array(
        'pageSize' => 10,
    ),
));

//view
<?php $this->widget('zii.widgets.grid.CGridView', array(
       'id' => 'students-grid',
       'dataProvider'=> $dataProvider, 
)); ?>
查看更多
forever°为你锁心
4楼-- · 2019-07-11 04:46

Make sure that your query output contains 'id' column.

查看更多
登录 后发表回答