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.
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>';
?>
<?
$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.
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.
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!
Use RSS to be notified when we publish an article. What is RSS?
Comments:
This article is no longer accepting comments.
"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.
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.