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

Tagged: , ,

Comments: 5

  1. Cesar August 18, 2010 at 01:22

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

  2. admin August 18, 2010 at 02:33

    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. pushparaj September 7, 2010 at 16:12

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

  4. tri July 5, 2011 at 14:48

    that is good article

  5. vhdm August 23, 2011 at 22:53

    Thanks!

Leave a Reply

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

*