Checking a condition in Yii Cgridview(Check infinite conditions)

While developing an application in Yii, i was held up in a situation where i needed to check for value from database and display value accordingly. I tried using the conditional operator :

(condition) ? "True":"False";

but then this is useful only for checking two values.. I had headaches trying to figure out the double quotes issues while using the conditional statements for checking 3 conditions.

I am an active member of the Yii forum and through that i got the perfect solution and i thought of sharing it here :

array(
    'name' => 'userType',
    'type' => 'raw',
    'htmlOptions'=> '',
    'headerHtmlOptions'=>'',
    'header' => 'User Type',
    'value'=>function($data){
        if ($data->userType  == 1){
            $class = 'Amin';
        }
        else if ($data->userType == 2){
            $class = 'Moderator';
        }
        else{
            $class = 'Users';
        }
        return $class;
    },
),

You can literally check infinite conditions using this and it is clean and follow the rules of Yii.

Cheers…:)

Leave a Reply

Your email address will not be published. Required fields are marked *