When you have a fully dynamic site it’s useful to show the user where they are by breaking down the URL structure so they can navigate backwards through the site. For example this url: http://papermashup.com/categories/jquery/ shows that ‘jquery’ is part of ‘categories’ so the user could navigate backwards through the url structure.
The first line of the function gets the REQUEST_URI (/path/to/file.php), splits the string (using ‘/’) into an array, then filters out any empty values. We then store the base url for the site in $base_url. Next we find the array key for the last value in the $path array. We then do a for loop to build up the breadcrumbs and determine where to put the a tag etc.
function breadcrumbs($separator = ' » ', $home = 'Home') {
$path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
$base_url = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
$breadcrumbs = array("<a href=\"$base_url\">$home</a>");
$last = end(array_keys($path));
foreach ($path AS $x => $crumb) {
$title = ucwords(str_replace(array('.php', '_'), Array('', ' '), $crumb));
if ($x != $last){
$breadcrumbs[] = '<a href="$base_url$crumb">$title</a>';
}else{
$breadcrumbs[] = $title;
}
}
return implode($separator, $breadcrumbs);
}
This is the simple bit! Simply add the above function to the top of your PHP page and call the function as shown below. You can optionally add a separator symbol or specify the wording for ‘Home’.
echo breadcrumbs();
Oh this is a really good tutorial, it’s just what I was looking for.
May be need add microformat or RDF to breadcrums for beautiful view in Google =).
Hi All,
Thanks for your post and reply also.
This is very usefull script.
I am learner, so can anyone help me out to learn jQuery and PHP.
Regards,
Deepak Raikwar
Your snippet didn’t worked for me. Breadcrumbs names are echoed right, but links to crumbs lower in the tree are all wrong and only works for single directory depth. So I made my version, check it out at http://codepad.org/McSUUNnA . I had some issues with $base_url, but I find this version the most compatible and it works on my local server and on a production server too, although codepad doesn’t get it right.
thanks hrvi your code works like a charm. I tried your code after i faced the same problem as you. thank you again.
Thanks Ashley, This is some nice piece of code to implement breadcrumbs.
Ah, html in the comments are not working ;(
So, in order to obtain working code check this http://cdpst.net/pfv1v9yp6 (it’s a code snippets share service)
Have a nice day.
Nice tip. Thanks. Thought there is an error in your code on line 12.
Replace that line by the following piece of code:
$breadcrumbs[] = ‘‘ . $title . ‘‘;
What’s the use of this bit? If a user has fully dynamic site with clean URLs it is most certainly operated by some sort of a CMS, that surely can do breadcrumbs. And if he hasn’t, this snippet won’t help anyway.
Designer and web developer, Co-founder and Technical Director at Harkable.com London. Previously I worked at InMobi, Spotify and MySpace. Interests include photography and making short videos. Also an avid F1 fan.
Follow us on Twitter and get in-stream updates.
Subscribe to all the Papermashup tutorials and articles straight to your RSS reader.
Sign up and get all the Papermashup tutorials straight to your inbox.
11 discussions around Display breadcrumbs on your site using PHP