Renu Thakur
Renu Thakur

Reputation: 551

Create pagination in yii framework

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?

Upvotes: 2

Views: 5567

Answers (1)

Danila Ganchar
Danila Ganchar

Reputation: 11223

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, 
)); ?>

Upvotes: 2

Related Questions