2

I saved some data in json format into my database in one field called json. I used widget in order to display full list of products.

 <?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'page-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        'id',
        'name',
        'json',
        array(
            'class'=>'CButtonColumn',
        ),
    ),
)); ?>

I decoded field (called json) which contains json data. And I am going to use a value of decoded json(p_1 element of decoded json (json["p_1"])) in the widget instead of whole field (called json).

(Because fields(called json) contains a lot of data and I need only one data). How can I use p_1 instead of field(called json)?

Regolith
  • 2,944
  • 9
  • 33
  • 50
phpdev
  • 511
  • 4
  • 22

1 Answers1

1

You can pass a php expression as the value to be displayed. $data represents the data model for the row.

'columns' => array(
    'id',
    'name',
    array(
        'name' => 'json',
        'value' => '$data->json["p_1"]'
    ),

Reference: http://www.yiiframework.com/doc/api/1.1/CDataColumn#value-detail

topher
  • 14,790
  • 7
  • 54
  • 70
  • It is not working. I used to retieve json data using following code $json["p_1"] ( Here $json=CJSON::decode($model->json);). when I inserted $ sign before json( 'value' => '$data->$json["p_1"]'), it did not work – phpdev Jan 06 '17 at 11:05
  • It looks like you aren't assigning the decoded json correctly. Does the code in your question show json in the `json` column? – topher Jan 06 '17 at 11:09