I see by the API that associative arrays can be used as data sources for the CDetailViews. I have created a CDetailView and passed it an associative array as the data parameter, but I cannot figure out how to populate the labels and values with the keys and values from the associative array. Also, I have built the associative array outside of the call to the CDetailView widget.
Anyone have any working examples or code on how this can be accomplished using associative arrays as the data source rather than models?
If it helps, the array I have built looks like:
Array
(
[spec 1] => value 1
[spec 2] => value 2
[spec 3] => value 3
[spec 4] => value 4
[spec 5] => value 5
)
and is called $specifications.
My CDetailView looks like:
<?php $this->widget(‘zii.widgets.CDetailView’, array(
'data'=> $specifications,
'attributes'=>array(
array(
'label'=>CHtml::encode($specifications),
'type'=>'raw',
'value'=>CHtml::encode($specifications)
),
),
)); ?>
How about this? Each attribute has a name that’s the hash key, with the value as the value. Just naming the attribute.
<?php
$specifications = array(
'spec1' => 'value1 hello',
'spec2' => 'value2 goodbye',
'spec3' => 'value3 blah',
'spec4' => 'value4 foo',
);
$this->widget('zii.widgets.CDetailView', array(
'data'=> $specifications,
'attributes' => array(
array(
'label' => 'spec1',
'value' => $specifications['spec1']
),
array(
'label' => 'spec2',
'value' => $specifications['spec2']
),
'spec3',
'spec4',
),
));
Thanks for the reply Steve.
Yes that does seem to be plausible, however, I don’t know what the key values are ahead of time. The associative array can change and will not always have the same key structure. I need a way to send the data array as the data source and then step through the array in the CDetailView pulling out the unique keys and their respective values.
compugator:
Yes that does seem to be plausible, however, I don’t know what the key values are ahead of time. The associative array can change and will not always have the same key structure. I need a way to send the data array as the data source and then step through the array in the CDetailView pulling out the unique keys and their respective values.
Well, if you don’t know the structure of your own data, there’s only so much you can do to structure your data.
But saying
'columns' => array_keys($specification)
will at least display all the fields.
Essentially I would like to be able to use the CDetailView to pull out the data similar to the following:
foreach( $specifications as $key => $value){
echo "key: $key, value: $value <br />";
}
I am just trying to figure out a way to reference the key and value for each row of the array from within the CDetailView widget.
The following works, but only returns the first row of data:
<?php $this->widget(‘zii.widgets.CDetailView’, array(
'data'=> $specifications,
'attributes'=>array(
array(
'label'=>CHtml::encode(key($specifications)),
'type'=>'raw',
'value'=>CHtml::encode($specifications[key($specifications)])
),
),
)); ?>
tri
(tri - Tommy Riboe)
May 19, 2011, 1:58pm
8
compugator:
…
What about this approach (you may have to add the type if ‘text’ isn’t appropriate)
$arr = array(
array('label'=>'spec 1', 'value'=>'value 1'),
array('label'=>'spec 2', 'value'=>'value 2'),
);
$this->widget('zii.widgets.CDetailView', array(
'data'=> array(), // dummy, to avoid error
'attributes'=>$arr,
));
/Tommy
Asgaroth
(Asgaroth Belem)
May 19, 2011, 2:16pm
9
compugator:
If it helps, the array I have built looks like:
Array
(
[spec 1] => value 1
[spec 2] => value 2
[spec 3] => value 3
[spec 4] => value 4
[spec 5] => value 5
)
and is called $specifications.
My CDetailView looks like:
<?php $this->widget(‘zii.widgets.CDetailView’, array(
'data'=> $specifications,
'attributes'=>array(
array(
'label'=>CHtml::encode($specifications),
'type'=>'raw',
'value'=>CHtml::encode($specifications)
),
),
)); ?>
I think your CDetailView should look like:
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=> $specifications,
'attributes'=>array(
'spec1',
'spec2',
'spec3',
'spec4',
'spec5',
),
)); ?>
Please take a look at the documentation
Yes that would work fine, but like I said, I do not know the key values up front. I have just decided to switch to using a database table and the CGridView to show the values.
Thanks everyone for their suggestions though.
jacmoe
(Jacob Moen)
May 20, 2011, 7:21pm
11
That’s not a problem - just replace the array with a function which returns an array.