PHP Breadcrumbs

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

What I would like:

Also, I get a syntax error if I try to add a class name.
ex. echo("<ul id="breadcrumbs">");

How about this? Again, copy exactly as is, forget previous code…
Edited with some improvements.

<?php
  $parts = explode("/", dirname($_SERVER['REQUEST_URI']));
  $filename = basename($_SERVER['REQUEST_URI']);
  $url = DIRECTORY_SEPARATOR;  
  echo("<ul>");
  echo("<li><a href=\"".DIRECTORY_SEPARATOR."\">Home</a>&nbsp;&nbsp;&blacktriangleright;</li>");  
  for($i = 1; $i < count($parts); $i++){	
    $url = $url . $parts[$i] . DIRECTORY_SEPARATOR;  
    echo("<li><a href=\"" . $url . "\">" . ucfirst(basename($url)) . "</a>&nbsp;&nbsp;&blacktriangleright;</li>");
  }      
  echo("<li><a href=\"" . $url . $filename ."\">" . ucfirst($filename) . "</a>&nbsp;&nbsp;&blacktriangleright;</li>");
  echo("</ul>");
?>

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)
image


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;

Thanks for working on this though!

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…

The code:

<?php
	show_breadcrumbs_ul($_SERVER['REQUEST_URI']);
	function show_breadcrumbs_ul($path){
		$parts = explode("/", dirname($path));
		$filename = basename($path);
		$url = DIRECTORY_SEPARATOR;  
		echo("<ul class=\"list_class\">");
		echo("<li class=\"listitem_class\"><a class=\"anchor_class\" href=\"".DIRECTORY_SEPARATOR."\">Home</a>&nbsp;&nbsp;&blacktriangleright;</li>");  
		for($i = 1; $i < count($parts); $i++){	
			$url = $url . $parts[$i] . DIRECTORY_SEPARATOR; 
			if($url != DIRECTORY_SEPARATOR){
			  echo("<li class=\"listitem_class\"><a class=\"anchor_class\" href=\"" . $url . "\">" . ucfirst(basename($url)) . "</a>&nbsp;&nbsp;&blacktriangleright;</li>");
			}
		}   
		$lastlink = $url . $filename;
		if($lastlink != DIRECTORY_SEPARATOR){
		  echo("<li class=\"listitem_class\"><a class=\"anchor_class\" href=\"" . $lastlink ."\">" . ucfirst($filename) . "</a>&nbsp;&nbsp;&blacktriangleright;</li>");
		}
		echo("</ul>");
	}
?>

It’s still not working.

image

Additionally, the links are also broken (They were before to, I just didn’t notice it).

<ul class="list_class"><li class="listitem_class"><a class="anchor_class" href="/">Home</a>&nbsp;&nbsp;&blacktriangleright;</li><li class="listitem_class"><a class="anchor_class" href="//"></a>&nbsp;&nbsp;&blacktriangleright;</li><li class="listitem_class"><a class="anchor_class" href="//contact">Contact</a>&nbsp;&nbsp;&blacktriangleright;</li></ul></div><div class="contact-text center">

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 = ' &raquo; ', $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>

Result:

image

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.