Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple dynamic breadcrumb

Tags:

I think this script is of big interest to any noob around here :) including me :)

What I want to create is a little code that I can use in any file and will generate a breadcrumb like this:

If the file is called "website.com/templates/index.php" the breadcrumb should show:

Website.com > Templates 

 ^^ link                    ^^plain text

If the file is called "website.com/templates/template_some_name.php" the breadcrumb should show:

Website.com > Templates > Template Some Name 

 ^^ link                   ^^link                ^^plain text

like image 671
Adrian M. Avatar asked Apr 07 '10 16:04

Adrian M.


People also ask

How do you make dynamic breadcrumbs?

Breadcrumbs dynamic navigation is implemented using various JavaScript functions like jQuery prepend(),clone() and click() methods. On click of each navigation link <a> , the child nodes are appended to its parent along with the “GeeksforGeeks /” link in the bottom div with the class display.


1 Answers

This may be overkill for a simple breadcrumb, but it's worth a shot. I remember having this issue a long time ago when I first started, but I never really solved it. That is, until I just decided to write this up now. :)

I have documented as best I can inline, at the bottom are 3 possible use cases. Enjoy! (feel free to ask any questions you may have)

<?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> <p><?= breadcrumbs(' > ') ?></p> <p><?= breadcrumbs(' ^^ ', 'Index') ?></p> 
like image 152
Dominic Barnes Avatar answered Oct 20 '22 05:10

Dominic Barnes