Many "if condition" in a cgridview ???

Hello everyone,

I started on Yii and I am faced with a problem.

I am looking to fill a Cgridview:

  • In a cell, I want that based on a value it displays an image.

The problem is that I have five possible values ​​so I can not adjust the following condition for the five options are in place:




     'value'=>'(Log::model()->find("content_id=$data->id")->status_id) == 1 ? CHtml::image("/images/vert.png") : CHtml::image("/images/rouge.png")',


I rephrase the question: How to manage 5 conditions in the ‘value’ of a cgridview

Thank you in advance for your help.

If you are fine with PHP 5.3+ only, define an anonymous function.


If you are still not on PHP 5.3…

you can create a new method in your model… and call it from the CGridView… something like


Then in getStatus() you can do all you need…

I was about to suggest the same, but I thought it’s not optimal MVC to put CHtml::image(…) in the model. I guess the method would be better suited in some “view helper” class.


If the method just returns the image name… than you can use something like:


Thank you for the track

In the end I made a feature of this style out of my widget and it goes well …

function getIt($id){


                $connection = Yii::app()->db;

                $sql = "'";

                $command = $connection->createCommand($sql);

                $data = $command->query();

                $row = $data->read(); 


                if ($row['...'] == '1' ) 


                        $voyantVert = CHtml::image("/images/vert.gif");

                        $value = CHtml::link(Yii::app()->request->baseUrl.$voyantVert, array("log/view", "id"=>$row['content_id']));


                if ($row['...'] == '2' ) 


                        $voyantRouge = CHtml::image("/images/orange.gif");

                        $value = CHtml::link(Yii::app()->request->baseUrl.$voyantRouge, array("log/view", "id"=>$row['content_id']));


                if ($row['...'] == '3' ) 


                        $voyantOrange = CHtml::image("/images/orange.gif");

                        $value = CHtml::link(Yii::app()->request->baseUrl.$voyantOrange, array("log/view", "id"=>$row['content_id']));


                if ($row['...'] == '4' ) 


                        $voyantGris = CHtml::image("/images/gris.gif");

                        $value = CHtml::link(Yii::app()->request->baseUrl.$voyantGris, array("log/view", "id"=>$row['content_id']));


                if ($row['...'] == '5' ) 


                        $voyantBlanc = CHtml::image("/images/blanc.gif");

                        $value = CHtml::link(Yii::app()->request->baseUrl.$voyantBlanc, array("log/view", "id"=>$row['content_id']));


                echo $value;            


in model make function like this.

	public function getButton(){

		if($this->regCount >= Yii::app()->params["max_registration"]){



			if (Yii::app()->user->isGuest){



				$ret=CHtml::link(Yii::t("ui","Join"),array("reg2", "id"=>$this->id));



		return $ret;


in gridview

$this->widget('zii.widgets.grid.CGridView', array(





[color="#006400"]/* moved to Yii 1.1 help forum */[/color]