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