Breadcrumbs navigation 1.0

Following the trail of breadcrumbs

What are "breadcrumbs"? you may ask. The name comes from the Hansel and Gretel fairy tale in which the pair used breadcrumbs to create a trail leading them out of the forest; the forest in this case is your website. Breadcrumbs are a trail of navigational links, that help a user determine where they are located in a hierarchical website.

The code that this script was originally posted to the PHP mailing list by Matt Williams -> Visit Homepage

PHP Code:

1. Takes the URL like of a webpage i.e. http://www.blank.com/void/warp.html
2. Create a link called 'Home' that will goto the home page of the site
3. Split the URL from the first slash
4. Create links to all directories (capitalise the first letter) and the current page
5. Print all the links together in appropriate order with a separator (in this case >): Home > Void > warp.html.

<?php
// path_info.php

// This function will show the path by directory
function show_path() {
global
$SERVER_NAME;
global
$PATH_TRANSLATED;
global
$SCRIPT_NAME;

/* Return the Script name as an array for the path menu i.e. home :subjects : etc.
Get rid of everything apart from the file name

strrchr ($SCRIPT_NAME, "/") returns portion of string that starts at the last occurrence of / and goes to the end of string
Substr returns the portion of string that begins after the length parameter (length of the string starts at 0, which would be /).
*/

$dir = substr(strrchr ($SCRIPT_NAME, "/"), 1);
//echo 'The file name - '.$dir; //can be unescaped to make sure script is working correctly

// Remove the file name from the scriptname by replacing the filename with
$path = str_replace("$dir", "", $SCRIPT_NAME);
//echo '<p>Removed filename from path - '.$path; //can be unescaped to make sure script is working correctly

// Remove the first and last /
$path = substr("$path", 1, -1);
//echo '<p>Removed the first and last / from path - '.$path; //can be unescaped to make sure script is working correctly

// Print out the navigation links
// The first href should be the full path for the home page of the site but the other links will be to only the site directories

//If the home page of your site is not accessible via a single "/" absolute URL, you should change the link to the appropriate URL
echo "<p><p><a class=link href=\"/\">Home</a> > ";

if(
$path != ""){

// Return the path as an array by spliting at /
$path = split( "/", $path);

if(
sizeof($path) != 0) {
$start = 0;
$stop = count($path); //number of elements in array

//$i < $stop has to be less than total number of elements in array because array starts at zero

for ($i = $start; $i < $stop; $i++) { //for all elements in the array until the end

//start printing the link
print "<a class=link href=\"/";

for (
$j = $start; $j <= $i; $j++) { //for each specific element print the value of the element

print "$path[$j]/";
}

//for each specific element print the value of the element with the first character in uppercase

print "\">".ucfirst($path[$i])."</a> > \n";
}
}

print
"<a class=link href=\"$dir\">$dir</a>"; //print link to current file
}

}

show_path();
?>

The path_info.php script should be included in each page of your website that will have breadcrumb navigation.

PHP Code:

<?php
include("path_info.php");
?>

See it in action. ¦ Get complete code.

Recommended reading:

Evolt.org has published Breadcrumbs for PHP Lovers

Jakob Nielsen's Useit.com Is navigation useful?

      Subscribe in a reader