PHP Breadcrumb Links Creator

Using PHP to automatically print a list of links relating to directories that can be used as web page breadcrumbs.

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.

Our PHP Breadcrumb Links Creator script maps the parent directories holding the file and prints out an unordered list of breadcrumb links with the default <ul id="crumbs"> so it can be styled easily.

Unstyled Example:

The Script has 2 Components:

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>';
?>

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.

Requirements

Meaningful directory names! /urg_sds/324b3/ won't make for good crumbs. If your directory names have meaning and all contain an index file then you're cooking on gas.

Extensibility

The script could also be useful to edit for integration within an existing CMS. The $pagetitle value could be a variable that could be printed automatically as the last crumb. This is how it will be used to automatically create breadcrumbs for GrowStore and GrowBlog.

Have fun!


More articles

Comments:

This article is no longer accepting comments.

  • On the 30th Jan 2007 at 17:35 GMT, Amdrew wrote:

"The $pagetitle value could be a variable that could be printed automatically as the last crumb. This is how it will be used to automatically create breadcrumbs for GrowStore and GrowBlog." I'd like to know more about this technique. If I have to manually type the page title, I'm not sure how useful this would be.

  • On the 28th Feb 2007 at 11:26 GMT, ionpositivo wrote:

Hi The Doctor, First of all I have to said that I found the article and the "hack" quiet handy though it doesn't fullfill my needs. Don't you think you could share your "approach" instead of just critisizing the one presented.