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
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.
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'),
),
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.