PHP Breadcrumb – Create breadcrumb in php


What are breadcrumbs?

Well quite simply, in web page terms this refers to a list of links (usually at the top) that appear on a web page to tell the user where they are in the structure of the website they are viewing. This makes navigating backward and forward so much easier as they have the ability to skip directly back through whole categories, rather than just using the browser’s back button.

Example

The breadcrumbs.php include file:

Copy the code below, save it in a file called breadcrumbs.php and upload it to your site. This will print out a simple unordered list (<ul>) of the parent directories by directory name. The only edits you may wish to make to this file is to change the $ul_id='crumbs'; line if you wish to change the id of the <ul>. If you wish to add a background image or border etc to the <li> simply select it as #crumbs li{} and style.

<?
$ul_id='crumbs';
$bc=explode("/",$_SERVER["PHP_SELF"]);
echo '<ul id="'.$ul_id.'"><li><a href="/">Home</a></li>';
while(list($key,$val)=each($bc)){
 $dir='';
 if($key > 1){
  $n=1;
  while($n < $key){
   $dir.='/'.$bc[$n];
   $val=$bc[$n];
   $n++;
  }
  if($key < count($bc)-1) echo '<li><a href="'.$dir.'">'.$val.'</a></li>';
 }
}
echo '<li>'.$pagetitle.'</li>';
echo '</ul>';

?> This is some more usefull code

<? 
$url = $_SERVER['REQUEST_URI'];
echo $url;
echo "<br>";
$array = explode("/",$url);
echo $array[2]; ///which folder would you want to access ?
echo "size of array = ".sizeof($array)."<br>";
?>

A script to insert in the HTML where you want the breadcrumbs to appear:

Edit the URI to point to where you saved the include file (once this is done it never needs to be changed again). Edit, ‘Insert Page Title’ to add the page title to the end of the breadcrumbs.

<?
$pagetitle="Insert Page Title";
include("http://www.yourdomain.com/breadcrumbs.php");
?>

Note: It would be useful to automatically print the page <title> as the last crumb but PHP cannot interogate the DOM. Javascript would be required which we didn’t think appropriate as nothing would be returned if Javascript was turned off.

Advertisements
This entry was posted in PHP and tagged , , by Purab Kharat. Bookmark the permalink.

About Purab Kharat

I am WordPress and Drupal Architect. I worked on many technologies like Ruby on Rails, Flex, PHP etc... I am Purab Kharat from India, Pune. I love to play chess and listen music and read books. I work in SY Smartsourcing Global Pvt Ltd in Princeton, NJ, USA. Now I am living at Plainsboro, 164 Hampshire Drive, Princeton Meadows, NJ. I have more then Ten years experience in Web Application Development (Specially in Php 5.0, Ruby on Rails, Mysql 5.0, RIA, Wordpress, Drupal, Joomla, Ecommerce)

16 thoughts on “PHP Breadcrumb – Create breadcrumb in php

  1. Hello,

    Can this script be used in any php based website or only on wordpress??

    I am a newbie and only learn web developemnt as a hobby and wanted to know how to create breadcrumbs for a website.

    any help will be much apprecaited

  2. thanks very much!
    Is there a way that I can make a group of pages belong to a submenu and then show the group right after the HOME link and being able to link to that page where the group list are shown.

    Thanks ,
    I hope i was clear

    Manu

  3. taking a quick glance, won’t your script leave a breadcrumb like the following:
    Home > Home
    on the homepage?

  4. What an extremely inspiring article. I am always sincerely impressed by your articles.
    You always post interesting ideas. Keep it up. Keep blogging.
    Really looking forward to going over your next posting.

  5. I blog quite often and I truly thank you for your content.
    Your article has really peaked my interest.

    I will bookmark your blog and keep checking for new details about once a week.
    I subscribed to your RSS feed as well.

  6. “It would be useful to automatically print the page as the last crumb but PHP cannot interogate the DOM”
    You could have php create the title as well, then you can use the title in your breadcrumbs

  7. Hey would you mind letting me know which hosting company you’re using? I’ve
    loaded your blog in 3 different internet browsers and I must say this blog
    loads a lot faster then most. Can you recommend a good
    web hosting provider at a reasonable price? Cheers, I appreciate it!

  8. Greetings from Ohio! I’m bored to tears at work so I decided to browse your website on my iphone during lunch break. I love the information you provide here and can’t wait to take a
    look when I get home. I’m shocked at how quick your blog loaded on my phone .. I’m not even using WIFI, just 3G
    .. Anyhow, good blog!

  9. Hello my loved one! I wish to say that this post is amazing, great written and come with approximately all vital
    infos. I would like to look more posts like this .

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s