Closer! Except the “Path” and “To” are still missing. Also, is there a way to capitalize the first letter of the filename (So the “A” in “about”?)
What it is now
Amazing, but is there any way I can add a class (For styling) to the “ul” and “li” elements? If I add it like normal echo("<ul class="classname>"); I get Parse error : syntax error in the browser.
Thanks!
Additionally, it shows an extra icon on any pages in the root folder (Like /contact)
Sorry, I found another thing. I wanted a line like this (That allows me to change the name) to be included. case "about": $label = "About Us"; break;
Ok. Now the code is in a function named show_breadcrumbs_ul($path)
You have to call it to execute it. In order to get the same result like before, you call it like this show_breadcrumbs_ul($_SERVER['REQUEST_URI']);
Otherwise you can call it with any path you want, for testing purposes as well. For example show_breadcrumbs_ul("/test");
The bug that you mentioned, with the extra breadcrumb, is now fixed.
The unordered list now has a class named list_class and the items have a class named listitem_class. I also added a class anchor_class for the anchors.
I don’t understand what you mean with the last request. This was just a gimmick of the previous coder that when it found a directory named ‘about’ in the path, it showed it as ‘About Us’ in the breadcrumbs. You couldn’t have used that in any way I think…
I used that to replace “Contact” with “Contact Us”, as well as a few other cases. I do want this feature, otherwise this one is no better than the working one that I found earlier.
The working one, in case it is easier to add the replace function than fixing the one above
Here it is. This one works well, except it does not have that search and replace function. If it could be added that would be great.
<?php
// This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
function breadcrumbs($separator = ' » ', $home = 'Home') {
// This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
$path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
// This will build our "base URL" ... Also accounts for HTTPS :)
$base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
// Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
$breadcrumbs = Array("<a href=\"$base\">$home</a>");
// Find out the index for the last value in our path array
$last = end(array_keys($path));
// Build the rest of the breadcrumbs
foreach ($path AS $x => $crumb) {
// Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
$title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));
// If we are not on the last index, then display an <a> tag
if ($x != $last)
$breadcrumbs[] = "<a href=\"$base$crumb\">$title</a>";
// Otherwise, just display the title (minus)
else
$breadcrumbs[] = $title;
}
// Build our temporary array (pieces of bread) into one big string :)
return implode($separator, $breadcrumbs);
}
?>
<p><?= breadcrumbs() ?></p>