• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
anchor tags

I've come across an interesting little puzzle. I want to put in an anchor tag using the url helper like this, largely because it automatically gets the link path right:

echo anchor('home','images/img.png');

but of course that only puts in 'images/img.png' as text. What I want to do is to be able to click on the image so I want to use the html helper code like:

echo img('images/img.png');

How can I embed one within another, or otherwise achieve the result?


I think this would do it!

echo anchor('home',img('images/img.png'));


Brilliant - thank you so much. Now I see it I don't know why I didn't try that - I tried lots of other ways.

Anyway, problem solved :-)

Or of course you could pass it the whole image tag if you need to apply styling or anything else (perhaps an id for some JS).

echo anchor('home', '<img src="images/img.png" id="topNavImage" style="height:20px" />');

// or if you need php in there as well (ie, image path - which is best to do this way incase
// you later move your files around and loose your path to your image folder, this way the path cannot be broken.

echo anchor('home','<img src="'.base_url().'images/img.png" id="topNavImage" style="height:20px" />');

Or you could presumably do:

echo anchor('home',img('images/img.png'),'id="something",class="somethingElse"');

Ah no - that would apply the attributes to the anchor I guess.

I get your point and thanks a lot for the insight.

ah sorry, I just re-read your initial post, the whole idea was to make use of the html helper in the first place! But it looks like you got what you needed from tintamarres answer. However, I mite have a play around with your code tonight when I get home from work, you could be onto something there, there must be a way to make it work! Maybe this...?

echo anchor('home',img('images/img.png',id='something',class='somethingElse'));

I tried that but php threw a parsing error...?

You could do this.

echo anchor('home',img(array('src' => 'images/img.png','id' => 'something','class' => 'somethingElse')));

How cool is that? I love it and works like a charm.

Thanks so much!

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.