CGridView not rendering image

Hi,

I have some problems rendering an image in CGridView. I have the following code:


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

	'dataProvider'=>$dataProvider,

	'columns'=>array(


		array(

                  'name'=>'Flag',

                    'type'=>'image',

                    'value'=>'CHtml::image("images/flags/".$data->country.".gif", "country")',

                ),


	),

));

$data->country contains the name of a country from my DB. So I have added the path and the extension of the image. However, the above code renders the following html:


<img alt="" src="<img src="images/flags/England.gif" alt="country" />"/>

I have found a solution to the problem by either declaring


'type'=>'raw',

or


'type'=>'html',

…but would like to know how to use the image type as I would like to use the proper settings of the framework.

Is there a bug in the code that is supposed to render an image or is there a bug in my head that has missed something in the above code making it not work?

Cheers,

b

Edit: The code is supposed to render the flag of the respective country.

What about this?




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

'dataProvider'=>$dataProvider,

  'columns'=>array(

    array(

      'name'=>'Flag',

      'type'=>'image',

      'value'=>'images/flags/'.$data->country.'.gif',

    ),

  ),

));



Edit: this should work too




      ...

      'value'=>array('images/flags/'.$data->country.'.gif', 'country'),

      ...



(not tested)

/Tommy

Hi Tommy,

your first code renders an empty <td/>

Your second suggestion returns:

include(images/flags/.gif.php) [<a href=‘function.include’>function.include</a>]: failed to open stream: No such file or directory

Source File

/home/yourownb/framework/YiiBase.php(338)

00326: * @param string class name

00327: * @return boolean whether the class has been loaded successfully

00338: include($className.’.php’);

Then I don’t know right now.

References I used:

http://www.yiiframework.com/doc/api/CGridView

http://www.yiiframework.com/doc/api/CDataColumn

http://www.yiiframework.com/doc/api/CFormatter

/Tommy

thanks for attempting to help anyway.

It can be done with ‘type’=>‘image’, but CFormatter::formatImage() will only pass one parameter to CHtml::image(), i.e. only url can be specified since CHtml::image doesn’t decode an array (which I wrongly assumed).

This should work (url only):




...

'value'=>'"images/flags/'.$data->country.'.gif"',

...



Note the double quotes.

/Tommy

I think it should be:




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

    'dataProvider'=>$dataProvider,

    'columns'=>array(

        array(

            'name'=>'Flag',

             'type'=>'html',

             'value'=>'CHtml::image("images/flags/".$data->country.".gif", "country")',

        ),

    ),

));



or:




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

    'dataProvider'=>$dataProvider,

    'columns'=>array(

        array(

            'name'=>'Flag',

             'type'=>'image',

             'value'=>'"images/flags/" . $data->country . ".gif"',

        ),

    ),

));



When "type" is "image", "value" must be link to the image.

This is not working either. It returns:


<img alt="" src="images/flags/.gif"/>

This has worked excellent:


'value'=>'"images/flags/" . $data->country . ".gif"',

thanks. Looks like the issue was with the formatting as Tommy suggested same strategy but I guess his formatting was incorrect such as mine.

Cheers guys,

bettor

Yeah, I should have mentioned that the $data->country was untested. Late hours -_-

Cheers

/Tommy

Any body now, how i can set the html attribute of image??

You can’t with the current implementation. Use type “raw” instead and use ‘CHtml::image(…)’ as value to create your custom image tag.

Try this one : (I already used it)




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

		'id'=>'user-grid',

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

		'filter'=>$model,

		'columns'=>array(

		  'loc_address',

			array('name'=>'loc_picture',

			  'type'=>'html',

			  'header'=>'Picture',

				'value'=> 'CHtml::link(CHtml::image($data->loc_picture, "image", array("width"=>30)), "/picture/noimage.jpg", array("class"=>"highslide"))'

			),

		),

	)); 



Your solution is amazing

Hi my uploaded file path is applcation/backups/status/

and gridview column array







	array('name'=>'status_icon',

            	'type'=>'image',

               	'value'=>'"$data->path". $data->status_icon ',

	),

i already set a attribute $path set in my model as


 $this->path = Yii::app()->baseUrl.'/backup/status/';

and its working fine for me

This should work well :




'value'=>function($data){

   return 'Your html code';

},



Yes, anonymous functons will work from PHP 5.3.

/Tommy

How to design this CGridView to display image and related field text information??

what css have to design?? or can any image extension work fine for it??

help me please i am working on this images are displaying fine but i need fine design to display…