Forum Moderators: coopster
Original post: [webmasterworld.com...]
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.
<?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;
}
?>
<p><?php echo createBreadNav($_SERVER['SCRIPT_NAME']); ?></p>
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>
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
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>';
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();