Changing data after it has been retrieved from the DB

I’m trying to change some data before displaying it to a CGridView. I’m not sure whether I should do this operation in the model or controller or even the view. Here is what I have but it is not working:

I’m getting a Parse error: syntax error, unexpected T_SWITCH




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

	'id'=>'properties-grid',

	'dataProvider'=>$dataProvider,

	'columns'=>array(

		array('type'=>'html','name' => 'name','value'=>  '"<b>" .CHtml::encode($data->name) ."</b><br />". 

		switch ($data->confidential){

			case 1:

			case 2:

				if( $data->city_desc) $data->city .= " ". $data->city_desc;

				$data->city  . ", " .$data->states->state_abbr ." ". $data->zip;

				break;

			default:

				$data->states->regions->region;

		}

		

		'),

		array('type'=>'html','name' => 'date_opened','value'=>  '"<div align=\"center\">" .CHtml::encode($data->date_opened). "</div>"'),

		

	)

)); 




Create a property in the model:

in the model add:




public function getNameConfidential()

{

 $return ="<b>" .CHtml::encode($data->name) ."</b><br />";

                switch ($data->confidential){

                        case 1:

                        case 2:

                                if( $data->city_desc) $return.=$data->city .= " ". $data->city_desc;

                                $data->city  . ", " .$data->states->state_abbr ." ". $data->zip;

                                break;

                        default:

                                $return.=$data->states->regions->region;

                }

                return $return;

}




In short words: a function that returns what you need.

Now in the CgridView you can use:




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

        'id'=>'properties-grid',

        'dataProvider'=>$dataProvider,

        'columns'=>array(

                array('type'=>'html','name' => 'name','value'=>  '$data->NameConfidential'),

                array('type'=>'html','name' => 'date_opened','value'=>  '"<div align=\"center\">" .CHtml::encode($data->date_opened). "</div>"'),

                

        )

)); 



Awesome, I did have to change the $data-> to $this-> in the function but works great now. I also had to add a ‘get’ at the beginning and a ‘()’ at the end of the:




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

	'id'=>'properties-grid',

	'dataProvider'=>$dataProvider,

	'columns'=>array(

		array('type'=>'html','name' => 'name','value'=>  '$data->getNameConfidential()'),

		array('type'=>'html','name' => 'date_opened','value'=>  '($data->date_opened=="0") ? "<div align=\"center\">N/A</div>" :"<div align=\"center\">" .CHtml::encode($data->date_opened). "</div>"'),		

	)

)); 

Here’s my finished function:




public function getNameConfidential()

{

         $return ="<b>" .CHtml::encode($this->name) ."</b><br />";

         switch ($this->confidential){

                        case 1:

                        case 2:

                                ($this->city_desc) ? $return.=$this->city . " ". $this->city_desc. ", " .$this->states->state_abbr:  $return.= $this->city . ", " .$this->states->state_abbr;

                                break;

                        default:

                                $return.=$this->states->regions->region;

         }

         return $return;

}