Using Switch In Cgridview Cssclassexpression

Is there any problem with the following array as a column in CGridView?


		array(

			'header' => 'Status',

			'value' => '$data->found_work',

			'cssClassExpression' =>

				'switch($data->found_work)

				{

					case \'Found\':

						return \'green\';

					case \'Not found\':

						return \'red\';

					case \'Wrong user\':

						return \'yellow\';

				}',

		),

I’m getting the error


Parse error: syntax error, unexpected 'switch' (T_SWITCH) in C:\xampp\yii\framework\base\CComponent.php(612) : eval()'d code on line 1

but don’t know why. It’s valid PHP code, does eval() not like switch statements?

Why do you need the switch inside the array? Can you try to remove the switch from the array and get the result of the switch statement before creating your array so that will will only look like this.




		

switch($data->found_work)

				{

					case 'Found':

						$classvalue = 'green';

					case 'Not found':

						$classvalue = 'red';

					case 'Wrong user':

						$classvalue = 'yellow';

				}

$array = array(

			'header' => 'Status',

			'value' => '$data->found_work',

			'cssClassExpression' => $classvalue

				

		),

No, because the data model object for that row is not available outside that array.

http://www.yiiframework.com/doc/api/1.1/CGridColumn#cssClassExpression-detail

Do something like this:




$arrayClass = array("Found" => "green", "Not Found" => "red", "Wrong User" => "yellow");


array(

                        'header' => 'Status',

                        'value' => '$data->found_work',

                        'cssClassExpression' => $arrayClass[$data->found_work]

                                

                ),



Appreciate the support, however that’s incorrect as well. $data is only valid when the expression is encased in quotes, it’s all around when that code gets evaluated. In your suggestion, $data is undefined at that point in time because it is still at the point where we are configuring CGridView. It needs to be encased in quotes so that as CGridView is running its internal methods, it evaluates $data at that point in time. This would work if I could somehow inject $arrayClass into the column (via a expressionData property, which does not exist).

I think we will just drop CGridView and use CListView as that allows for much more flexible rendering possibilities.