[solved] [newbie] CHtml:link with an image


I have (I hope so) a simple question…

How do I create a link with an image?

The normal way is so:

<?php echo CHtml::link([some HTML], array('view', 'id'=>$data->id)); ?>

The problem is the following:

The pictures I’d like to show are saved on my server, the path is saved in a mySQL-Database.

Here is the structure:

id int(11) auto_increment

picturenumber int(11) -> imagename on the server

gallery int(11) -> FK to another table (at the moment not relevant)

portfolio bool

How the pictures are stored:


And now the big question…

What have I to insert in the server/protected/views/gallery/_view.php?

It must be something like this ;)

<?php echo CHtml::link(<img src=“gallery/”$data->gallery/th_$data->picturenumber.jpg" alt="$data->picturenumber.jpg" />, array(‘view’, ‘id’=>$data->id)); ?>

And the output must be like this:

<a href="server/index.php?r=pictures/view&id=[id]">

  <img src="server/gallery/[$data->gallery]/[$data->picturenumber].jpg" alt="[$data->picturenumber]">


[size=“4”]Thanks in advance! :D[/size]

You answered yourself, just have to use appropriate apostrofe…

echo CHtml::link('<img src="server/gallery/'.$data->gallery.'/th_'.$data->picturenumber.'.jpg" alt="'.$data->picturenumber.'.jpg" />', array('view', 'id'=>$data->id));

or for more Yii way

$imghtml=CHtml::image('server/gallery/'.$data->gallery.'/th_'.$data->picturenumber.'.jpg', $data->picturenumber.'.jpg');

echo CHtml::link($imghtml, array('view', 'id'=>$data->id));

That works perfect! :D

Thanks! :D :lol:

Please am building a simple student record management system. furthermore, i want to use an image as a link to my log in page on my web application. tried using the default "img src" tag which i know is wrong so how do i go about it. i created my web application using the Yiic tool so my image is already in images folder. please help


The answer is spelled out in one of the previous posts - what seems to be the problem?

I reckon you were refering to pocra’s reply. would try it now an give you some feedback.


Mdomba spelled it out in two ways. :)

What about CHtml:link with an image and a confirm message?

I tried it but there is something wrong:

$image = CHtml::image( 'images/x.png', 'Delete', array('title'='Delete'));

echo CHtml::link($image, '#', array('confirm' => 'Are you sure you want to delete this item?'));