CodeIgniter Forums

Full Version: Several html elements inside an anchor tag, with CI's anchor funtion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Joures[/eluser]
Hi,

i'm trying to get an image tag and a span tag inside an anchor tag using
CI's anchor function, to no success. Anyone has any idea how to do this?
I've succeeded in getting the image inside the tags but can't figure out how
to get the span inside there too.

Help greatly appriciated.

El Forum

[eluser]Bart Mebane[/eluser]
I haven't tried it, but you should be able to use any text you want, including html tags. How about
Code:
&lt;?php echo anchor('news/article', '<img src="picture.jpg" /><span>some text</span>') ?&gt;

El Forum

[eluser]InsiteFX[/eluser]
you can also pass a third parameter as an associative array.

InsiteFX

El Forum

[eluser]Joures[/eluser]
[quote author="InsiteFX" date="1291200222"]you can also pass a third parameter as an associative array.

InsiteFX[/quote]

As kind of a noob to php in general i have to ask, how would one do that?

El Forum

[eluser]LuckyFella73[/eluser]
By passing an associative array you can add attributes to
the link tag - for example:
Code:
&lt;?php
$array = array('class'=>'test');
echo anchor("home", "Link", $array);
// would produce:
<a class="test" href="http://localhost/your_app/home">Link</a>
?&gt;
This way you can set a css class (to set a background image for example)

See: http://ellislab.com/codeigniter/user-gui...elper.html

El Forum

[eluser]teampoop[/eluser]
You can also use functions that output text inside the second argument, which will output the same text as Bart mentioned..
Code:
&lt;?php echo anchor('news/article', img('picture.jpg').'<span>some text</span>') ?&gt;

On a side note, the third parameter on the anchor function is an array that would only change the attributes of the <a> tag. Not affect the innerHTML.

again, I could be wrong.

El Forum

[eluser]Dennis Rasmussen[/eluser]
If anchor() makes it too complicated for you then don't use it :S
It's just a helper for quick tags.

El Forum

[eluser]TwoConnect[/eluser]
Personally, I stay away from the anchor tag. It makes things 10 times more complicated in my opinion

El Forum

[eluser]techgnome[/eluser]
Really? I find the opposite true. Especially when it comes to making links within a loop where very little changes. I can set the assoc array once, loop while passing the text and link info, and the array. Done. But eh... to each their own.

-tg