Forum Moderators: coopster

Message Too Old, No Replies

Automated Breadcrumbs Generation Script

inspired by PHP Bag-O-Trick, Enhanced by WebmasterWorld Community

         

le_gber

6:45 pm on Jun 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First of all, credits must go to grahamstewart who posted an inspiring code in one of the original PHP-Bag-O-Tricks threads.

Original post: [webmasterworld.com...]



I am not the greatest PHP coder / developer and would therefore appreciate any input on the following script by people who know what they are doing. Once it's been cleaned up and improved, it will off course be free to used by anybody.

It is working fine as is, but I just would like to streamline it. It works on all level of the site (from the homepage to x directories) and display a non-link anchor for the page your are on. It can also be quite easily customised to 'look' different on different sites.



Put the following in a php file that you include at the top of each page:

<?php
function createBreadNav($strThePath) { // create the breadcrumb navigation
// config variables
$dirIndexes = 'index.php'; //name of directory index files (index, default etc...)
$filenameSeparators = '_'; //how do you split your file names (_ or -)
$breadCWords = 'You are in: '; //starting words for the breadcrumbs
$homeLinkWord = 'Home'; //first link anchor text (Home, Acceuil, etc...)
$delim = ' : '; //delimiters for the breadcrumbs

// check if the path is of path format and return lowercase version if ok
if(preg_match('/[\/]+[-_a-z0-9\/]+(\.php)?+(.*)?$/i', $strThePath)){
$cleanPath = htmlentities(strtolower($strThePath), ENT_QUOTES);
}else{
exit('<h1>Error. File path of incorrect format.</h1>');
}

//create new variables to hold path and links data
$pathArr = array();
$pathLink = '/'; //start from root
$breadcrumbs = ''; //the echoed variable at the end
$lastFile = ''; //the file name

// put path data into the array
$pathArr = substr($cleanPath, 1); //remove first / of path
$pathArr = explode("/", $pathArr);

if( sizeof($pathArr) == 1 ){//we are on root level
if(end($pathArr) == $dirIndexes){ // we are on the homepage
$breadcrumbs = $breadCWords . $homeLinkWord;
}else{ // we are not on the homepage but still root level
$breadcrumbs .= $breadCWords . '<a href="/'.$dirIndexes.'">' . $homeLinkWord .'</a>' . $delim . substr(ucwords(str_replace($filenameSeparators, ' ', $pathArr[0])), 0, -4);
}

}else if(sizeof($pathArr) > 1){// we are in a directory

$lastFile = array_pop($pathArr); //remove last array value and place it in another var
$lastFile = substr($lastFile, 0, -4);// removes the .php extension of the filename

$breadcrumbs .= '<p>'. $breadCWords . '<a href="/'.$dirIndexes.'">' . $homeLinkWord .'</a>';

foreach($pathArr as $dir){ // loops throught the directories in the path array
$pathLink .= $dir . '/';
$pathAnchor = ucwords(str_replace($filenameSeparators, ' ', $dir));
$breadcrumbs .= $delim . '<a href="'. $pathLink . $dirIndexes.'">'.$pathAnchor.'</a>';
}
if($lastFile != substr($dirIndexes, 0, -4)){ // file is not index file
$breadcrumbs .= $delim . ucwords(str_replace($filenameSeparators, ' ', $lastFile)); // adds the file name to the breadcrumbs
}
}

return $breadcrumbs;

}
?>



and to call the function simply paste the following on your pages:

<p><?php echo createBreadNav($_SERVER['SCRIPT_NAME']); ?></p>



Looking forward to some input :)

cameraman

7:38 pm on Jun 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That looks pretty streamlined to me.

henry0

11:30 am on Jul 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



le_gber,
Great breadcrumps

As is it works fine, however when you are in a situation like root/index.php/any_dir/aa.php
dir: any_dir shows in the breadcrumbs not only its dir name but as an activated link, if you click on that dir the result is a 404, so you might eventually make all directories non activated?
Or much more complicated :) open (on mouse over) that none activated dir and make all pages included in that dir an activated link

Last I propose to add some sort of tag for page/s that should not be seen

Your thoughts?

<edit> OOPS! the "tag" does not mean anything since to show a page needs to include your script
</edit>

le_gber

11:58 am on Jul 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi henry0,

I did try to recreate the 'root/index.php/any_dir/aa.php' situation but couldn't see the directory appear in the breadcrumbs. Did you physically create these directories or is that part of a mod_rewrite / more complex server config?

henry0

12:21 pm on Jul 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi le_gber,
no, it is simply an existing complete site that lives on my local WAMP test bed
here is what I did:
not even included the script but pasted it on top of index.php
then called it as per your snippet

and got:(I copied it from the test result)
<<<
You are in: Home : Site Name: Main : Discip
>>>
main is a "main dir" which contains more files and dirs
the file maned "Discip" (inside main) is the only one iside that dir
to received your script
the dir "main" is showing as an activated clickable link.

sructure:
from root
index.php/main/discp.php

le_gber

7:20 am on Jul 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



henry0,

thanks for your detailed explanation. I guess that this would be one of the automated script limitation. It assumes that there is an index.php page in every directory. Do you have an .htaccess that is configured to handle DirectoryIndex as being something else than index.php

The reason I am asking is because I have forced the script to add index.php at the end of every URL to avoid duplicate content (ie. the breadcrumbs having /dir1/dir2/ as links and the other /dir1/dir2/index.php) We could remove the forced index.php if the htaccess handles the directoryIndex.

ps. I also noticed that there is a <p> tag that needs removing towards the end of the script:

$breadcrumbs .= '<p>'. $breadCWords . '<a href="/'.$dirIndexes.'">' . $homeLinkWord .'</a>';

henry0

11:40 am on Jul 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks le_gber
no you are correct I do not have that htaccess setup
so could we envision adding a condition looking for such htaccess.

one of the reason I do not use an index in most dir is due to link friendly for SE, better have "here_is_my_stuff.php" than index all over again
and mostly I want to be sure that an user will not try to randomly open any dir/
so yes I have an index (that I renamed for test purpose) that if one tries to directly open a dir/
reads "you do not have access here..." exit();