Codeigniter: Helper – Create breadcrumb from url

Breadcrumb is a navigational tool that developer often integrate in their application to give easy access to different segment of the application. This is used in applications which has multiple sequence of activities.

The following code will go into your helper file and you have to call create_breadcrumb(); function to get the formatted html text;

  1. if(!function_exists(‘create_breadcrumb’)){
  2. function create_breadcrumb(){
  3.   $ci = &get_instance();
  4.   $i=1;
  5.   $uri = $ci->uri->segment($i);
  6.   $link = ‘<ul>’;
  7.  
  8.   while($uri != ”){
  9.     $prep_link = ”;
  10.   for($j=1; $j<=$i;$j++){
  11.     $prep_link .= $ci->uri->segment($j).’/';
  12.   }
  13.  
  14.   if($ci->uri->segment($i+1) == ”){
  15.     $link.=’<li>» <a href="’.site_url($prep_link).’"><b>’.$ci->uri->segment($i).’</b></a></li> ’;
  16.   }else{
  17.     $link.=’<li>» <a href="’.site_url($prep_link).’">’.$ci->uri->segment($i).’</a></li> ’;
  18.   }
  19.  
  20.   $i++;
  21.   $uri = $ci->uri->segment($i);
  22.   }
  23.     $link .= ‘</ul>’;
  24.     return $link;
  25.   }
  26. }
  27.  

If the current url is http://www.domain.com/user/add then the create_breadcrumb() function will return -

  1. » user » ad

Here user is linked to http://www.domain.com/user/ and add is linked to http://www.domain.com/user/add

This is generated by calling in the view file. Try it out and let me know how it went :)

thanks,

thephpx

[ad code=1 align=center]

Share

About thephpx

I am a freelance web application developer. I work with PHP5 and MySQL. Most of my projects are done using the codeIgniter framework.I am also working towards moving onto the Zend Framework. I follow agile development methodology and manage my projects throuh SCRUM based approach. Apart from being a developer I am also the CEO of a small web identity and application development firm located in dhaka, bangladesh. The firm known as 'implode impressions' also follows agile development techniques and works under the SCRUM based approach.

5 responses to “Codeigniter: Helper – Create breadcrumb from url

  1. Hello! That’s an interesting topic but.. which helper? Can I extend it?

  2. This is a custom helper, you need to create a custom helper file then copy the provided code. Also make sure you auto-load the required helpers before you load the custom helper.

    thanks,

    thephpx :)

  3. how to make a dynamic breadcrumb without session and without url trap can you please help me

  4. tri

    that is good article

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>