I found this working php code to create a breadcrumb. But as long as my localhost root is not the local website one, I have a too deep, so to say, home (root).
How should I modify the code to get the correct home in local?
I guess I should modify this piece of code:
// This will build our "base URL" ... Also accounts for HTTPS :)
$base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
But this other code doesn’t word
$base = $root;
(where $root is correctly settled as the root of a single local, or remote, website)
Doesn't work is not a particularly useful problem description. Perhaps you could start by providing the results of the original $base variable as well as your new and improved $root variable. And then perhaps post an example of an improper breadcrumb.
Note also that the comments for the gist indicates at least one problem with nested directories. Make sure you are not running into this issue.
I get as root link not the root of my specific local website (localhost > web > mywebsite), but localhost: ‘home’ link (of a specific local website) is localhost and not localhost > web > mywebsite.
Not sure to understand, but every specific local website has a specific $root variable: so local website A has as $root localhost / web / A, and local website B has as $root localhost / web / B.
Indeed in the root folder I define $root=".";
in a subfolder I have $root="..";
in a sub-subfolder I have $root="../..";, and so on.
Yes, although you can change the local development root to make it easier to develop a website locally. For example, I do this when using MAMP on my Mac for local development.
I don’ t know MAMP…
But maybe the simplest (even quite dirty) solution could be use if ($_SERVER['SERVER_NAME'] === "localhost") { [some rule to cut the first two link] }
How can I cut the first two link of the whole local path?
I have a (last?) problem:to avoid the two » » at left, I have modified the code: » no more from php, but from css.
At least, it should be from css. But this code doesn’t work:
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("<span><a href=\"$base\">$home</a></span>");
// Initialize crumbs to track path for proper link
$crumbs = '';
// 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', '_', '%20'), array('', ' ', ' '), $crumb));
// If we are not on the last index, then display an <a> tag
if ($x != $last) {
$breadcrumbs[] = "<span><a href=\"$base$crumbs$crumb\">$title</a> »</span>";
$crumbs .= $crumb . '/';
}
// Otherwise, just display the title (minus)
else {
$breadcrumbs[] = $title;
}
}
// Build our temporary array (pieces of bread) into one big string :)
return implode($separator, $breadcrumbs);
}
// Change » to >
if ($_SERVER['SERVER_NAME'] === "localhost") {
echo " <div id='breadcrumblocal'>";
echo breadcrumbs(' ');
echo " </div>";
}
else {
echo " <div id='breadcrumb'>";
echo breadcrumbs(' ');
echo " </div>";
}