I pulled data from my API in JSON format
and convert to a CArrayDataProvider in my controller.
$arraydata = array();
for ($i=$start_element ; $i<$start_element+$num_elements; $i++) {
$arraydata[$i] = array(
"id" => $obj->response->publishers[$i]->id,
"name" => $obj->response->publishers[$i]->name
);
}
The data looks like this (using print_r();
CArrayDataProvider Object
(
[keyField] => id
[rawData] => Array
(
[0] => Array
(
[id] => 121422
[name] => Default Real Time
)
[1] => Array
(
[id] => 122328
[name] => Rogue
)
[2] => Array
(
[id] => 122330
[name] => ARTIST
)
[3] => Array
(
[id] => 122510
[name] => The Rugged
)
...
When I call index.php
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'itemView'=>'_view',
));
In _view.php
<div class="view">
<?php echo 'ID:'; ?>
<?php echo $data->id;
?>
</div>
I get an error saying
"Trying to get property of non-object"
But don’t understand why.
If I remove the second line, the array of 100 gets printed out to the view just fine as
"ID:"
"ID:"
…
What am I missing
seenivasan
(Chellamnivas)
#2
Dear Friend
If I am perceiving correctly,
It should be like this.
_view.php
<div class="view">
<?php echo 'ID:'; ?>
<?php echo $data["id"]; ?>
<?php echo "</br>";?>
<?php echo 'NAME:'; ?>
<?php echo $data["name"]; ?>
</div>
Thank you my friend !
I just found some other reference to a problem similiar.
I am not certain if I created this problem by not building my array the correct way or if this is just how it works with JSON.
Thanks again !
seenivasan
(Chellamnivas)
#4
Dear Friend
This is nothing related with JSON .
This is between php array and CArrayDataProvider.
Below is one example involving associative array.
$arr=array(
array('id'=>1,'profile'=>array('name'=>'jack','age'=>10,'sex'=>'male')),
array('id'=>2,'profile'=>array('name'=>'jill','age'=>8,'sex'=>'female')),
array('id'=>3,'profile'=>array('name'=>'jhon','age'=>6,'sex'=>'male')),
array('id'=>4,'profile'=>array('name'=>'jerry','age'=>4,'sex'=>'male')),
);
?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'profile-grid',
'dataProvider'=>new CArrayDataProvider($arr,array(
'sort'=>array(
'attributes'=>array('id','profile.name','profile.age','profile.sex')
),
'pagination'=>array('pageSize'=>2)
)),
'columns'=>array(
'id',
array(
'header'=>'Name',
'name'=>'profile.name',
'value'=>'$data["profile"]["name"]'
),
array(
'header'=>'Age',
'name'=>'profile.age',
'value'=>'$data["profile"]["age"]',
),
array(
'header'=>'Sex',
'name'=>'profile.sex',
'value'=>'$data["profile"]["sex"]'
),
),
));
?>
Just copy it in a view.
Regards.
Thank you !
It is working, just thought I might have been doing something wrong in building my array from the JSON.
I have many services (api’s) that I need to pull data from. I am trying to find a way to do so without
having to explicitly code the arrays. I would like to build a model or some structure as a class file to allow
me to use functionality for mapping the arrays into my data and doing things like printing attribute labels
in a manner similiar to they way the models work in active record.
It’s turned into a bigger project than I imagined and a bit overwhelming.