How to make a link using Codeigniter.
Here we learn how to use anchor method that is URL helper of Codeigniter.We already discussed in my previous post URL structure of Codeigniter. It creates a anchor tag in HTML view.
We can create anchor links in two ways.
- by using anchor function available in URL helper of Codeigniter.
- by using site_url function available in URL helper of Codeigniter.
Below examples shows how to make HTML link Using anchor function
anchor tag with two parameters,parameter 1 takes segments of the targe page,parameter 2 takes link text.
1 2 3 4 | <?php echo anchor('controller/method/parameter1', 'Link Text'); // <a href="http://example.com/index.php/controller/method/parameter1">Link Text</a> ?> |
anchor tag with additional attributes like title
1 2 3 | <?php echo anchor('controller/method/parameter1', 'Link with Title attribute',array('title'=>'Link Title'));// <a href="http://example.com/index.php/controller/method/parameter1" title="Link Title">Link with Title attribute</a> ?> |
anchor tag with javascript attribute.
1 2 3 4 5 | <?php echo anchor('controller/method/parameter1', 'Link with Title and javascript',array('title'=>'Link Title','onclick'=>"alert('hi');return false;")); //<a href="http://example.com/index.php/controller/method/parameter1" title="Link Title" onclick="alert('hi');return false;">Link with Title and javascript</a> ?> |
remember anchor tag won’t create internel page link.
Below examples show how to make HTML link using site_url function
We can use site_url output as href attribute and can make internel links also.
1 2 | <p><a href="<?php echo site_url('controller/method/parameter1'); ?>">with site_url function </a></p>
<p><a href="<?php echo site_url('controller/method/parameter1'); ?>" title="link title">with site_url function and title attribute </a></p> |
Test it in your local system
To test these functions ,just download the example files and paste into your codeigniter root directory. run the example.
if you have any queries you can mail me.
Preview
Popularity: 44% [?]










Related Articles
8 users responded in this post
[...] 36. How to make a link using Codeigniter [...]
[...] 36. How to make a link using Codeigniter [...]
[...] 36. How to make a link using Codeigniter [...]
I want to thank the blogger very much not only for this post but also for his all previous efforts. I found http://www.maheshchari.com to be very interesting. I will be coming back to http://www.maheshchari.com for more information.
[...] How to make a link using Codeigniter [...]
[...] 36. How to make a link using Codeigniter [...]
[...] 36. How to make a link using Codeigniter [...]
[...] How to make a link using Codeigniter [...]
Leave A Reply