Help With Application

Hello everyone, I’m having a hard time trying to figure out how to display conditional images in my cGridView…

I started creating a model with my data base… in my data base I have a column name unique_id this is created with the php code of for unique ids. And I have name the images bases on that unique id… basically unique_id1.jpg

But not all of the records has an image related, so I want to display an image that says no image, that is with the name 23.jpg. All of this files are contained on the folder BaseUrl/files/

so here is the code that I have in views/admin.php


<?php 


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

	'id'=>'prop-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,


	'columns'=>array(

		

		//Images

		

		array (

			'name'=>'prop_id_unique',

			'header'=>'Image',

			'type'=>'html',

			//'value'=>'CHtml::image(Yii::app()->baseUrl."/files/".$data->prop_id_unique."1.jpg","prop_id_unique",array("width"=>130))',

			//'value'=>$model->getImage(),

			//'value'=>'(file_exists(Yii::app()->baseUrl."/files/".$data->prop_id_unique."1.jpg"))?CHtml::image(Yii::app()->baseUrl."/files/".$data->prop_id_unique."1.jpg","prop_id_unique",array("width"=>130)):"no image"',

			//'value'=>'CHtml::link(CHtml::image(Yii::app()->baseUrl."/files/".$data->prop_id_unique."1.jpg","prop_id_unique",array("width"=>130)),Yii::app()->baseUrl."files/23.jpg"',

			'htmlOptions'=>array('style'=>'text-align:center'),

		),



under each // are the tries I have tried to solve this, but the onlyone working is the first one that doesn’t display the conditional formation to display 23.jpg.

Can someone help me with this???

Okay this is how I would approach, I don’t like my views to have complex logic if it more complex then I would probably move it to a widget here is simple and quick solution




// in your model

public function afterFind()

	{

		if ($this->hasAttribute('image') && empty($this->image))

			$this->image = Yii::app()->baseUrl.'/files/no-image.jpg';


		return parent::afterFind();

	}


// update your gridview attribute like so and you are done


	array (

		'name'=>'prop_id_unique',

		'header'=>'Image',

		'type'=>'html',

		'value'=>'CHtml::image($data->image)',

		'htmlOptions'=>array('style'=>'text-align:center'),

	),

hope it helps

Thanks, I’m trying to do that, but it gives me error. So I tried the following codes.




// in my model


public function image($uniqueid)

	{

		$src = Yii::app()->baseUrl."/files/".$uniqueid."1.jpg";

		if (file_exists($src))

	 	{

	 	$image = CHtml::image(Yii::app()->baseUrl."/files/23.jpg",$uniqueid,array("width"=>130));

	 	}

		

		Else

		{

			$image = CHtml::image($src,$uniqueid,array("width"=>130));

		}

	

		return $image;

	}


// in my view

			'name'=>'Image',

			'header'=>'Image',

			'type'=>'html',

			'value' => '$data->image($data->prop_id_unique)',

			






but the file_exists is not working it just returns the else function.

Thank you everyone for helping… I was able to resolve the issue with the following code:




// view


'columns'=>array(

		

		//Images

		

		array (

			'header'=>'Image',

			'type'=>'html',

			'value' => '$data->image($data->prop_id_unique)',

			'htmlOptions'=>array('style'=>'text-align:center'),

		),


// model


public function image($uniqueid)

	{

		if (file_exists(YiiBase::getPathOfAlias('webroot').'/files/'.$uniqueid.'1.jpg'))

	 	{

	 		$image = CHtml::image(Yii::app()->baseUrl."/files/".$uniqueid."1.jpg",$uniqueid,array("width"=>130));

		}

		

		Else

		{

			$image = CHtml::image(Yii::app()->baseUrl."/files/23.jpg",$uniqueid,array("width"=>130));

			

		}

	

		return $image;

	}