Skip to content

How to display image in CGridView in YII

Last updated on February 10, 2018

This post is a simple example of showing images in CGridView in Yii.Imagine that you have an image field in your table ie either a location field or a blob type field used to store the images.

Here is the simple example

widget('zii.widgets.grid.CGridView', array(
	'id'=>'banners-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
	'columns'=>array(
		'banner_id',
		array(
	      'name'=>'banner_img', 
              'type' => 'raw',
              'value' => 'CHtml::image(Yii::app()->baseUrl . "/banner/home/" . $data->banner_img,"", array("width"=>200, "height"=>100))',
           ),
		'banner_url',	     
		array(
			'class'=>'CButtonColumn',
		),
	),
)); ?>

You can also directly set type to image in config array as shown below ,

array(
       'name'=>'banner_img', 
        'type'=>'banner_img',
        'value'=>'Yii::app()->request->baseUrl."/uploads/banners/".$data->banner_img','htmlOptions'=>array('width'=>'100px','height'=>'50px'),
        ),

That’s it !.

0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments