Yii Framework Forum: condition in view file - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

condition in view file Rate Topic: -----

#1 User is offline   neel 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 133
  • Joined: 08-October 09

Posted 01 March 2010 - 10:59 AM

Hello,
I need a condition in my view file but don't understand how to do that

this is my view file

<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
'id',
'name',
array(
'name'=>'phone',
'type'=>'image',
'value'=>'if ($data->phone=0) . "images/red.gif"',
),


'email',
),


));
?>
0

#2 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 01 March 2010 - 11:06 AM

'value' => '($data->phone == 0) ? "images/red.gif" : ""',


See here under "Ternary Operator".
0

#3 User is offline   yiimann 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 102
  • Joined: 19-December 09

Posted 01 March 2010 - 11:09 AM

I'm sorry but I dont dont understand the meaning of your code.


if ($data->phone=0) . "images/red.gif"'

0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 01 March 2010 - 11:16 AM

View PostY!!, on 01 March 2010 - 11:06 AM, said:

'value' => '($data->phone == 0) ? "images/red.gif" : ""',


See here under "Ternary Operator".


Minor Typo: You should not have ' around that expression.

'value' => $data->phone==0 ? 'images/red.gif' : '',

0

#5 User is offline   neel 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 133
  • Joined: 08-October 09

Posted 01 March 2010 - 11:34 AM

View PostMike, on 01 March 2010 - 11:16 AM, said:

Minor Typo: You should not have ' around that expression.

'value' => $data->phone==0 ? 'images/red.gif' : '',



'value' => '($data->phone == 0) ? "images/red.gif" : ""',
This is working fine. but I need else $data->phone == 1 then images/green.gif
0

#6 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 01 March 2010 - 11:35 AM

View PostMike, on 01 March 2010 - 11:16 AM, said:

Minor Typo: You should not have ' around that expression.

'value' => $data->phone==0 ? 'images/red.gif' : '',



See here.
0

#7 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 01 March 2010 - 11:37 AM

View PostNeel, on 01 March 2010 - 11:34 AM, said:

'value' => '($data->phone == 0) ? "images/red.gif" : ""',
This is working fine. but I need else $data->phone == 1 then images/green.gif


Look at the link I posted. You can do this:

'value' => '($data->phone == 0) ? "images/red.gif" : "images/green.gif"',

0

#8 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 01 March 2010 - 11:39 AM

View PostY!!, on 01 March 2010 - 11:35 AM, said:

See here.


Ah, of course. Missed that. :)
0

#9 User is offline   neel 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 133
  • Joined: 08-October 09

Posted 01 March 2010 - 11:40 AM

View PostY!!, on 01 March 2010 - 11:37 AM, said:

Look at the link I posted. You can do this:

'value' => '($data->phone == 0) ? "images/red.gif" : "images/green.gif"',


Thanks. working :rolleyes:
0

#10 User is offline   intel352 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 196
  • Joined: 05-February 10
  • Location:Southport, NC

Posted 01 March 2010 - 12:03 PM

Just a note, when writing a ternary, it's generally good practice to wrap the entire ternary in parens to avoid issues.

( ($data->phone == 0) ? "images/red.gif" : "images/green.gif" )

Need live Yii support? - Join the #yii IRC channel on Freenode!
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users