Check 3 Conditions In Yii Cgrid View?

Is it possible to check 3 conditions in the cgridview column ??

I have done the below one for checking two conditions :




array(

 'name' => 'active',

 'type' => 'raw',

 'htmlOptions'=> array('class'=>'width50'),

 'headerHtmlOptions'=>array('class' => 'width50'),

 'header' => 'Active',

 'value' => '(($data->active == 1) ? "Yes" : "No")',

),     



Is it possible like :




array(

 'name' => 'active',

 'type' => 'raw',

 'htmlOptions'=> array('class'=>'width50'),

 'headerHtmlOptions'=>array('class' => 'width50'),

 'header' => 'Active',

 'value' => '(($data->active == 1) ? "Yes" : " check another condition here ? ")',

),   



Or any suggestions for the best way to get around this ?

Of course, whatever you write there is evaluated as PHP, and you can nest ternary operators as much as you want.


 'value' => 'expr1 ? expr2 : (expr3 ? expr4 : (expr5 ? expr6 : expr7) : expr8)',

If you do it too much it becomes hard to read though.

You can also just add a method to your model that returns the text and use ‘$data->methodName()’ instead.

So basically it would be like this ? :




array(

 'name' => 'active',

 'type' => 'raw',

 'htmlOptions'=> array('class'=>'width50'),

 'headerHtmlOptions'=>array('class' => 'width50'),

 'header' => 'Active',

 'value' => '(($data->active == 1) ? "Yes" : " ($data->active == 0) ? "No" : "Nil" ")',

),  




How will i go around the quotes (") issue ?..:(.

Also You can use annonymous function, for example:


                            'value'=>function($data){

                                        if ($data->active  == 1){

                                            $class = 'Yes';

                                        }

                                        else if ($data->active == 0){

                                            $class = 'No';

                                        }

                                        else{

                                            $class = 'Nil';

                                        }

                                        return $class;

                                    },

And it seems a little bit more elegant for me.

Awesome Mirunho,… Worked like magic… This was the method i was looking for … Just didnt know i could just write a function there… I was thinking of going all the way back to controller for this…:P

Thanks you guys Mirunho & Tsunami… Cheers…

Glad to help, have a nice day.