Yii Framework Forum: CGridView not rendering image - Yii Framework Forum

Jump to content

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

CGridView not rendering image Yii1.1 Rate Topic: -----

#1 User is offline   bettor Icon

  • Standard Member
  • PipPip
  • Group: Members
  • Posts: 220
  • Joined: 02-February 09

Posted 07 February 2010 - 12:14 PM

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

#2 User is offline   tri Icon

  • Master Member
  • PipPipPipPip
  • Group: Members
  • Posts: 653
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 07 February 2010 - 12:31 PM

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

This post has been edited by tri: 07 February 2010 - 12:39 PM

0

#3 User is offline   bettor Icon

  • Standard Member
  • PipPip
  • Group: Members
  • Posts: 220
  • Joined: 02-February 09

Posted 07 February 2010 - 12:41 PM

View Posttri, on 07 February 2010 - 06:31 PM, said:

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');
0

#4 User is offline   tri Icon

  • Master Member
  • PipPipPipPip
  • Group: Members
  • Posts: 653
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 07 February 2010 - 12:54 PM

View Postbettor, on 07 February 2010 - 07:41 PM, said:

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.yiiframew...c/api/CGridView
http://www.yiiframew...api/CDataColumn
http://www.yiiframew.../api/CFormatter

/Tommy
0

#5 User is offline   bettor Icon

  • Standard Member
  • PipPip
  • Group: Members
  • Posts: 220
  • Joined: 02-February 09

Posted 07 February 2010 - 01:03 PM

thanks for attempting to help anyway.
0

#6 User is offline   tri Icon

  • Master Member
  • PipPipPipPip
  • Group: Members
  • Posts: 653
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 07 February 2010 - 08:46 PM

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

This post has been edited by tri: 07 February 2010 - 09:27 PM

0

#7 User is offline   Richard Dam Icon

  • Newbie
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 07-February 10

Posted 07 February 2010 - 10:54 PM

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

#8 User is offline   bettor Icon

  • Standard Member
  • PipPip
  • Group: Members
  • Posts: 220
  • Joined: 02-February 09

Posted 08 February 2010 - 05:29 AM

View Posttri, on 08 February 2010 - 02:46 AM, said:

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



This is not working either. It returns:
<img alt="" src="images/flags/.gif"/>

0

#9 User is offline   bettor Icon

  • Standard Member
  • PipPip
  • Group: Members
  • Posts: 220
  • Joined: 02-February 09

Posted 08 February 2010 - 05:31 AM

View PostRichard Dam, on 08 February 2010 - 04:54 AM, said:

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

#10 User is offline   tri Icon

  • Master Member
  • PipPipPipPip
  • Group: Members
  • Posts: 653
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 08 February 2010 - 06:04 AM

View Postbettor, on 08 February 2010 - 12:31 PM, said:

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
0

#11 User is online   rudenich Icon

  • Newbie
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 30-April 10

Posted 02 May 2010 - 06:20 AM

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

#12 User is offline   Mike Icon

  • Elite Member
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,674
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 02 May 2010 - 08:37 AM

View Postrudenich, on 02 May 2010 - 02:20 PM, said:

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

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