jstockdale
(Joshdstockdale)
March 3, 2011, 3:48am
1
I am having a bit of trouble manipulating the data before displaying it in the CGridView.
I’ve got this code for the other columns which works great:
$this->widget(‘zii.widgets.grid.CGridView’, array(
‘id’=>‘properties-grid’,
‘dataProvider’=>$dataProvider,
‘columns’=>array(
array(‘type’=>‘html’,‘name’ => ‘rooms’,‘value’=> ‘"<div align=\“center\”>" .CHtml::encode($data->rooms). “</div>”’)
)
));
But the problem comes in when I want to check if the value is equal to 0 then change the data accordingly. For instance, here is my php code:
($data->price == ‘0’) ? $data->price = ‘Contact Us for pricing information’: $data->price = ‘$’ . number_format($data->price, 0);
How do I perform this if statement before displaying the data?
mdomba
(Maurizio Domba Cerin)
March 3, 2011, 7:47am
2
Your expression needs to return a value that will be displayed… so… just do not assign the value to the variable… like
$data->price=='0' ? 'Contact Us for pricing information' : '$'.number_format($data->price,0);
jstockdale
(Joshdstockdale)
March 3, 2011, 1:01pm
3
But the issue is using that in context of the
columns =>array( value=>
[/quote]
mdomba
(Maurizio Domba Cerin)
March 3, 2011, 1:17pm
4
What is the issue?
This should work
array(
'name'=>'price',
'value'=>'$data->price=="1" ? "Contact Us for pricing information" : "$".number_format($data->price,0)',
),
Note: in this example the expression should be enclosed with single quotes as we want that $data->price is evaluated at runtime…
jstockdale
(Joshdstockdale)
March 3, 2011, 2:28pm
5
Well, I guess there isn’t. It works now. Thanks a lot.
When I was doing it last night the value would either be ‘Contact Us for pricing’ for all of them or all would show price and so some said $0. This framework is awesome by the way, just have to learn the lingo.
bhbrayeun
(Bhbrayeun)
March 6, 2012, 7:07am
6
mdomba:
What is the issue?
This should work
array(
'name'=>'price',
'value'=>'$data->price=="1" ? "Contact Us for pricing information" : "$".number_format($data->price,0)',
),
Note: in this example the expression should be enclosed with single quotes as we want that $data->price is evaluated at runtime…
what if I want to compare 3 values ? like
switch($data->tipe){
case Benda::TIPE_FOOD:
return Benda::TIPE_FOOD_STR;
case Benda::TIPE_SAUCE:
return Benda::TIPE_SAUCE_STR;
case Benda::TIPE_VEG:
return Benda::TIPE_VEG_STR;
}
What kind of expression should I use ?
Any help would be most appreciated.