Pull data from json and display it in Gridview

I have a ison file http://xxx.com/api/feed/xxxxx.json?multi_brand=1

below is the info…


items: [


brand: {

tid: "xxx",

name: "xxx",

description: null,

logo: "xxx",

cover: ""


nid: "xxx",

title: "xxx",

summary: "xxx ",

social_channel: "xxx",

post_type: "xxx",

url: "xxx",

comment_count: "0",

author: {

profile: "xxx",

name: "xxx",

picture: "xxx"


featured: "0",

status: "1",

created: "xxx",

datetime: "xxx",

datetime2: "xxx",

content_type: [ ],

source: {

id: "xx"


I need to display some info like NID , Title and etc in grid view. How to i pull the data and display it?

in controller

        $data = array();

	$data = file_get_contents('http://xxx.com/api/feed/xxxxx.json?multi_brand=1');

	$data = json_decode($data, true);

but in view

So how to show it zii.widgets.grid.CGridView?

CGridView takes data from its dataProvider property which is an IDataProvider interface.

This interface is implemented by CArrayDataProvider class.

$data = array();

            if(isset($_GET['Model'])) {

            $data = file_get_contents('http://xxx/api/feed/499912.json?multi_brand=1');

            $data = json_decode($data, true);



        $rawdata = array();

        if (!empty($data)) {

            foreach($data as $array) {

                $rawdata[] = $this->arrayToObject($array);





        $dataProvider = new CArrayDataProvider($rawdata, array(



		 'dataProvider' => $dataProvider,




     private function array_to_obj($array, &$obj) {

     foreach ($array as $key => $value) {

            if (is_array($value)) {

                $obj->$key = new stdClass();

                array_to_obj($value, $obj->$key);

            } else {

                $obj->$key = $value;



        return $obj;


    private function arrayToObject($array) {

        $object = new stdClass();

        return $this->array_to_obj($array, $object);


and in my view file i try to display some data like title, thumbnail but no result pop out.

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



    'selectableRows' => 0, //disable highlight



                'header' => 'No.',

                'value' => '++$row',

                'htmlOptions'=>array('width'=>'10px','style'=>'text-align:right;padding:0px 10px;'),



                'header' => 'Title',

                'type' => 'raw',

                'value' => '"<strong>Title: </strong>".$data->title',




                'header' => 'Image',

                'type' => 'raw',

               'value' => '"<image src=\"$data->thumbnail\" style=\"max-height:150px;\">"',



Could you dump $data, $rawdata variables in your controller and $dataProvider variable in your view?

You can remove or censor out the sensitive data.

Ya array (0) @_@ fainted

Your data gets lost between file_get_contents() function and CGridView widget in view?

$data = array();

if(isset($_GET[‘Modelname’])) {

$data = file_get_contents(‘http://xxx/api/feed/499912.json?multi_brand=1’);

$data = json_decode($data, true);



here already gone

if i didn;t using if(isset($_GET[‘Modelname’]))

data not lost …weird

Hi man, listen don’t complicate yourself, make use of JQuery, take the data (with Ajax if you need or want to) and apply the serialize() function. That’s all.

Here’s another one: you can get the data server-side from a file or a query (it doesn’t matters), then using client-side script with JQuery use getJSON() to get it and later serialize() to put it in the grid.

Hey if is there any doubt yet replay and keep the dialog.

Hei ya…any sample coding can be reference? Sorry i’m new to yii

Don’t worry bro, let me prepare one and I’ll post it

Thank you very much

Hi bro, here some code…

I suggest you to make use of components to write some JavaScript routines that’s a healthy practice. So, I’m gonna give you a quick example using Ajax with an action (on php server-side of course) and JQuery on the client side.

This is on the view with the grid with id MyGrid…

Yii::app()->clientScript->registerScript(‘dataScript’, "

function upData_click() {

    var url = 'index.php?r=MyExample/getJSonData';


            url: url, 

            type: 'post', 

            success: function(resp){ 

	$('#MyGrid-grid').yiiGridView('update', {

		resp: $(this).serialize()


	return false;


            error: function(){

               	alert('it's been an error);






Could be with this option… it would be the same…

$.getJSON(action, function(listJson) {…})

This is the action on the controller…

public function actionGetJSonData($id)


$model = MyModel::model()->find('ID = ?', array($id));

header("Content-type: application/json");

echo CJSON::encode($model);


I hope can be usefull… this way of help is too pragmatic. I recomend you some material to support these code lines in a theorical way that is very important to know. I can send you some links…

Keep comunication…

thank you !