Yii Framework Forum: Help With Application - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Help With Application I need to make conditional images in my CGrid Rate Topic: -----

#1 User is offline   elpioramirez 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 31-July 14

Posted 31 July 2014 - 08:28 AM

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???
0

#2 User is offline   alirz23 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,092
  • Joined: 08-August 12
  • Location:Durban, South Africa

Posted 01 August 2014 - 01:02 AM

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
0

#3 User is offline   elpioramirez 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 31-July 14

Posted 01 August 2014 - 11:54 AM

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.
0

#4 User is offline   elpioramirez 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 31-July 14

Posted 01 August 2014 - 11:02 PM

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;
	}


0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users