Home : Webmonkey : Code : Breadcrumbs : Position Crumbs : Php

introduction

It's much nicer to do some things at the server side. When you do your processing on the server, you can just send HTML to the browser. This means that you don't have to worry quite so much about browser differences with regards to JavaScript.

The following code demonstrates position breadcrumbs, implemented in PHP. You may also want to check out history breadcrumbs

example

home | webmonkey | code | breadcrumbs | position_crumbs

code

To preserve maintainability, I recommend that you try to segregate content from code. What I tend to do is have one or more 'functions' php files, which get included at the top of pages. I put as much code as is sensible into functions and then call the functions where they're needed in the page. If a function needs to be changed, I change it on one place only. It also makes maintaining content easier, if you're not constantly tip-toeing around chunks of code.

Drop the code into a file called functions.php. Be sure to include the functions.php file in each page with:

<?php include ( $_SERVER['DOCUMENT_ROOT']  . '/includes/functions.php'); ?>

Now, at the spot where the crumbs are to appear, include:

<?php breadcrumbs(); ?>

The contents of functions.php file follows:

<?php
/*
Copyright Justin Whitford 2006.
  http://www.whitford.id.au/
Perpetual, non-exclusive license to use this code is granted
on the condition that this notice is left in tact.
*/
function breadcrumbs(){
  $bread = explode('/', $_SERVER['PHP_SELF']);
  $url = '/';
  $returnString = "<span class='bc0'><a href='$url'>home</a>";
  for($i=1;$i<count($bread)-1;$i++){
    $url.=$bread[$i].'/';
    $returnString .= " |</span> <span class='bc$i'><a href='$url'>$bread[$i]</a>";
  }
  echo $returnString.'</span>';
}
?>

discussion

This tiny function uses a PHP variable which gives the virtual path to the page executing the script (check the PHP manual to make sure this is how you access the variable for the version of PHP that you're using). The first instinct would be to use a split function on the path to populate an array with the directories, but a quick bit of investigation shows that explode is a better choice in this instance.

variation #1

Perhaps you'd like to spruce up the directory names, by removing underscores and %20s. Perhaps you'd like to present the crumbs in title case.

Home | Webmonkey | Code | Breadcrumbs | Position Crumbs

variation #1 code

This time we've added a 'prettify' function to functions.php.

<?php
/*
Copyright Justin Whitford 2006.
  http://www.whitford.id.au/
Perpetual, non-exclusive license to use this code is granted
on the condition that this notice is left in tact.
*/
function breadcrumbs(){
  $bread = explode('/', $_SERVER['PHP_SELF']);
  $url = '/';
  $returnString = "<span class='bc0'><a href='$url'>Home</a>";
  for($i=1;$i<count($bread)-1;$i++){
    $url.=$bread[$i].'/';
    $returnString .= " |</span> <span class='bc$i'><a href='$url'>"
      .prettify($bread[$i])."</a>";
  }
  echo $returnString.'</span>';
}
function prettify($dirName){
  $dirName = str_replace('_', ' ', $dirName);
  $dirName = str_replace('%20', ' ', $dirName);
  return ucwords(strtolower($dirName));
}
?>

variation #2

People typically ask for crumbs that vary from the actual directory names. You can do that, but you're making a rod for your own back. For every new directory, you need a new corresponding 'nice' label.

Here's how to do it, anyway...

Home | Fee | Fie | Foe | Position Crumbs

variation #2 code

<?php
/*
Copyright Justin Whitford 2006.
  http://www.whitford.id.au/
Perpetual, non-exclusive license to use this code is granted
on the condition that this notice is left in tact.
*/
function breadcrumbs(){
  $bread = explode('/', $_SERVER['PHP_SELF']);
  $url = '/';
  $returnString = "<span class='bc0'><a href='$url'>Home</a>";
  for($i=1;$i<count($bread)-1;$i++){
    $url.=$bread[$i].'/';
    $returnString .= " |</span> <span class='bc$i'><a href='$url'>"
      .prettify($bread[$i])."</a>";
  }
  echo $returnString.'</span>';
}
function prettify($dirName){
  $directoryName['webmonkey']='Fee';
  $directoryName['code']='Fie';
  $directoryName['breadcrumbs']='Foe';
  if($directoryName[$dirName]!=''){
    $dirName=$directoryName[$dirName];
  }else{
    $dirName = str_replace('_', ' ', $dirName);
    $dirName = str_replace('%20', ' ', $dirName);
    $dirName = ucwords(strtolower($dirName));
  }
  return $dirName;
}
?>

variation #3

Don't have your own domain? No problem...

Home | Breadcrumbs | Position Crumbs

variation #3 code

<?php
/*
Copyright Justin Whitford 2006.
  http://www.whitford.id.au/
Perpetual, non-exclusive license to use this code is granted
on the condition that this notice is left in tact.
*/
function breadcrumbs(){
  $startAtCrumb = 3;
  $url = '/webmonkey/code/';
  $bread = explode('/', $_SERVER['PHP_SELF']);
  $returnString = "<span class='bc0'><a href='$url'>Home</a>";
  for($i=$startAtCrumb;$i<count($bread)-1;$i++){
    $url.=$bread[$i].'/';
    $returnString .= " |</span> <span class='bc$i'><a href='$url'>"
      .prettify($bread[$i])."</a>";
  }
  echo $returnString.'</span>';
}
function prettify($dirName){
  $dirName = str_replace('_', ' ', $dirName);
  $dirName = str_replace('%20', ' ', $dirName);
  return ucwords(strtolower($dirName));
}
?>