Problem with CgridView

I am passing a variable($xxx) from contoller

print_r($xxx) shows the bellow mentioned output:




Array

(

    [A] => Array

        (

            [id] => 2

            [name] => DB

        )


    [B] => Array

        (

            [id] => 1

            [name] => AI

        )


)

In view i want to show the data in a Gridview and i am using




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

  'dataProvider'=>$xxx,

    'columns'=>array(

        '$xxx.id', // also used 'id'

        '$xxx.name',     // also used 'name'

    ),

));

But getting a error:

Fatal error: Call to a member function getData() on a non-object

Any suggestion will be appreciated…

You have to pass a data provider to your grid view, like shown below and $xxx has to be a model.




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

  'dataProvider'=>new CArrayDataProvider($xxx),

  'columns'=>array(

                array(

                    'name'=>'name',

                    'header'=>'Name',

                ),

                array(

                    'name'=>'price',

                    'header'=>'Price',

                ),

    ),

));




@nightmove

Thanks for your reply.

But new error arise

Undefined offset: 0

Can you show me how your code looks like?

In controller:


public function actionIndex()

	{       

                $cart=yii::app()->cart->contents();

                //echo '<pre>';

                //print_r($cart);

		$this->render('index', array('cart'=>$cart));

	}

In view:




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

  'dataProvider'=>new CArrayDataProvider($cart),

    'columns'=>array(

                array(

                    'name'=>'id',

                    'header'=>'id',

                    'value'=>'CHtml::encode($data["id"])'

                ),

                array(

                    'name'=>'name',

                    'header'=>'Name',

                    'value'=>'CHtml::encode($data["name"])'

                ),

                array(

                    'name'=>'price',

                    'header'=>'Price',

                    'value'=>'CHtml::encode($data["price"])'

                ),

     

    ),

));



You can set your column values like this:




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

  'dataProvider'=>new CArrayDataProvider($cart),

    'columns'=>array(

                array(

                    'name'=>'id',

                    'header'=>'id',

                    'value'=>'CHtml::encode($data->id)'

                ),

                array(

                    'name'=>'name',

                    'header'=>'Name',

                    'value'=>'CHtml::encode($data->name)'

                ),

                array(

                    'name'=>'price',

                    'header'=>'Price',

                    'value'=>'CHtml::encode($data->price)'

                ),

 	

    ),

));



Furthermore I do not understand your controller code. Why don’t you use:


public function actionIndex()

	{   	

                $cart=new Cart();

                //echo '<pre>';

                //print_r($cart);

		$this->render('index', array('cart'=>$cart));

	}

First of all its a ECommerce(shopping cart) application.

Cart is a component class which uses session data.

Add,delete,update is ok to the Cart.

Through


$cart=yii::app()->cart->contents();

I am taking the data in the Cart and passing it to the index.


$cart=new Cart();

It also works like


$cart=yii::app()->cart->contents();

.

Does it work with this? not sure

(it is not a CActiveDataProvider)

it will work if it comes from a model


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

  'dataProvider'=>$xxx->search(),

    'columns'=>array(

        '$xxx.id', // also used 'id'

        '$xxx.name',     // also used 'name'

    ),

));



Actually there is no model for this case.

Thank you.

Oh, if there is no model then I don’t know how this works with a grid view. I am passing models (or data providers) to my grid views. Maybe someone else can help.

Sry!

Thanks to all those who have spent time for helping me to solve the problem.

Finally i have done it using


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

  'dataProvider'=>new CArrayDataProvider(array_values($data)),

    

    'columns'=>array(           

            

                array(

                    'name'=>'id',

                    'header'=>'id',

                    'value'=>'$data["id"]'

                ),

                array(

                    'name'=>'name',

                    'header'=>'Name',

                    'value'=>'$data["name"]'

                ),

                

    ),

));