Please Note:
While this function should still work, I have written a new one which you can download directly at
http://pack.abstractflow.com/php/bre...breadcrumb.zip it functions much like this one. Instructions are included in the file. Please let me know of any issues with this new breadcrumb function.
Notes before starting
It is assumed you already know how to use PHP on your webpages. The basics of knowing a '.php' extension, and calling a function and passing variables to it. Potentially how to use includes, and in that case knowing how to navigate a filesystem.
First off, what is a breadcrumb?
This is easily answered with an example. Let's say we are currently visiting, say this address:
Code:
http://www.example.com/foo/bar/file.php?get=data#fragment
The breadcrumb serves the purpose of breaking this down. After it does that, it can output links to each directory, the root, and the current file. This is is a navigation feature suitable for any website.
An example output of that URL would be:
Home /
Foo /
Bar /
file.php
If you hover over those links you should be able to see each location in your status bar (and if not just click the links). It should be self-explanatory how this can be useful for navigating a website.
Now to get to the actual code.
PHP Code:
<?php
function breadcrumb(
$home = 'Home', // Name of root link
$division = ' / ', // Divider between links
$hidextra = true, // Toggle hide/show get data and fragment in text
$index = false, // Toggle show/hide link to directory if it does not contain a file
$indexname = 'index.php' // The definition of the file the directory must contain
) {
$whole = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$parts = explode('/', $whole);
$parts[0] = 'http://'.$parts[0];
$breadcrumb .= '<a href="'.$parts[0].'">'.$home.'</a>'.$division;
$k = 1;
for ($i=1;$i < sizeof($parts);$i++) {
$uri = '/';
while ($k <= $i) {
$uri .= $parts[$k];
if ($k != (sizeof($parts)-1)) $uri .= '/';
$k++;
}
if ($index && is_dir($_SERVER['DOCUMENT_ROOT'].$uri) && is_file($_SERVER['DOCUMENT_ROOT'].$uri.$indexname)
|| !$index
|| !is_dir($_SERVER['DOCUMENT_ROOT'].$uri)) {
$breadcrumb .= '<a href="'.$uri.'">';
if ($hidextra) {
$breadcrumb .= implode('', array_slice(explode('?', ucfirst($parts[$i])), 0, -1));
}
else {
$breadcrumb .= ucfirst($parts[$i]);
}
$breadcrumb .= '</a>';
}
else {
$breadcrumb .= ucfirst($parts[$i]);
}
if (isset($parts[($i+1)])) {
$breadcrumb .= $division;
}
$k = 1;
}
return $breadcrumb;
}
?>
Complicated? No fear, no editing of that portion is needed. You simply need it in every file that you want the breadcrumb to appear on. For ease, you can create a new file "breadcrumb.php" and include() this file onto your pages. This way you don't have to copy and paste it over and over. However, even though this is optional, I will still use it in my example.
So let's assume I have my file "breadcrumb.php" with the above function in it. Now I have another file. To use the breadcrumb I am going to include it.
newfile.php
PHP Code:
<?php
include 'breadcrumb.php';
?>
Keep note of directories when including a file.
Now I need to call the function. The function already has some default values. You may or may not change these when calling the function. here is a rundown...
The first argument is the name of the "root link". So the link to
http://www.example.com will be masked as "Home", or to whatever you specify.
The second argument is the divider between links. So between Home and newfile.php there will be a forward slash.
The third argument says whether to hide get data and fragments from the TEXT of the file link. The get data and fragment will still be in the HREF in the HTML source. This feature was added because some sites use really long get data strings, and this would break a page since it is usually one solid line of text.
The fourth argument toggles "file sensitivity". (true is on and false is off) This is meant to be used to check if there is an index file in the directory before making a link to it. The text will still be displayed, just not an HTML link surrounding it.
The fifth argument is the filename the fourth argument checks for. See if there is no index file in a directory and the breadcrumb makes a link to it, if someone clicks on that link they will either get a directory listing (potentially bad) or a 403 error. If the breadcrumb checks to make sure the index file exists first, the link will not show up and only the text. However the way it has been setup, the breadcrumb can actually check for any file in any directory before making the link. **NOTE: to never display a link for a directory, simply require the function to look for a file that will never exist.
Feel free to change the defaults in the function! This will save time when calling the breadcrumb() function if your arguments are already there.
newfile.php
PHP Code:
<?php
include 'breadcrumb.php';
echo breadcrumb();
?>
Try that out! Make some dummy directories to see the breadcrumb in action.
If you want to override the defaults you have set, you must override them in the order they were set in the function. The argument order is still the same.
Try out these calls:
PHP Code:
echo breadcrumb('Bring Me Home', ' // ');
PHP Code:
echo '{'.breadcrumb('Root', '}{').'}';
PHP Code:
echo breadcrumb('Home', ' \ ', false, true);
Be creative, and enjoy!