Display SQL result

Hi there!

I would like to display result of SQL query in something like CGridview. But Cgridview accept only a list of item and I can only have a list of array with CSqlDataProvider. Is there an other widget which display data after SQL query?

Thanks!

Hi,

it should be possible to pass any C*DataProvider to the gridview. It doesn’t matter if it is a CSqlDataProvider or a CActiveDataProvider.

CGridView

Here is my code:


$count=Yii::app()->db->createCommand('SELECT COUNT(*) contract')->queryScalar();

$sql='SELECT * FROM contract';

$dataProvider=new CSqlDataProvider($sql, array(

    'totalItemCount'=>$count,

    'sort'=>array(

        'attributes'=>array(

             'f_contract_id','f_site_id', 'f_expiry_date', 'f_price_unit',

        ),

    ),

    'pagination'=>array(

        'pageSize'=>10,

    ),

));

// $dataProvider->getData() will return a list of arrays.


$this->widget('zii.widgets.grid.CGridView', array(

        'dataProvider'=>$dataProvider->getData(),

        'columns'=>array(

                'f_contract_id',

                'f_site_id',

                'f_expiry_date',

                'f_price_unit',


        )));

Like this I get the following error:

Without getData() I get this error:

Have you an idea?

See this thread.

/Tommy

Thanks! It’s exactly what I need!

You could also use CArrayDataProvider instead of SqlDataProvider if you hasn’t linked your query to a particular model :

Check this thread

Many thanks This was just what I was looking for!!!!