CodeIgniter Forums
Several html elements inside an anchor tag, with CI's anchor funtion - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Several html elements inside an anchor tag, with CI's anchor funtion (/thread-36393.html)



Several html elements inside an anchor tag, with CI's anchor funtion - El Forum - 11-30-2010

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


Several html elements inside an anchor tag, with CI's anchor funtion - El Forum - 11-30-2010

[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;



Several html elements inside an anchor tag, with CI's anchor funtion - El Forum - 11-30-2010

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

InsiteFX


Several html elements inside an anchor tag, with CI's anchor funtion - El Forum - 12-01-2010

[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?


Several html elements inside an anchor tag, with CI's anchor funtion - El Forum - 12-01-2010

[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-guide/helpers/url_helper.html


Several html elements inside an anchor tag, with CI's anchor funtion - El Forum - 12-01-2010

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


Several html elements inside an anchor tag, with CI's anchor funtion - El Forum - 12-02-2010

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


Several html elements inside an anchor tag, with CI's anchor funtion - El Forum - 12-02-2010

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


Several html elements inside an anchor tag, with CI's anchor funtion - El Forum - 12-02-2010

[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