Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 2.26 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Breadcrumbs not working
  2. hxxp://fakesite.com/-->Home
  3. hxxp://fakesite.com/A-->A
  4. hxxp://fakesite.com/B-->B
  5. hxxp://fakesite.com/C-->C
  6.        
  7. hxxp://fakesite.com/-->Home
  8. hxxp://fakesite.com/A-->A
  9. hxxp://fakesite.com/A/B-->B
  10. hxxp://fakesite.com/A/B/C-->C
  11.        
  12. <?php
  13.  
  14. // This function will take $_SERVER['REQUEST_URI'] and build a breadcrumb based on the user's current path
  15. function breadcrumbs($separator = ' &raquo; ', $home = 'Home') {
  16. // This gets the REQUEST_URI (/path/to/file.php), splits the string (using '/') into an array, and then filters out any empty values
  17. $path = array_filter(explode('/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)));
  18.  
  19. // This will build our "base URL" ... Also accounts for HTTPS :)
  20. $base = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
  21.  
  22. // Initialize a temporary array with our breadcrumbs. (starting with our home page, which I'm assuming will be the base URL)
  23. $breadcrumbs = Array("<a href="$base">$home</a>");
  24. $last = end(array_keys($path));
  25.  
  26. // Find out the index for the last value in our path array
  27. $last = end(array_keys($path));
  28.  
  29. // Build the rest of the breadcrumbs
  30. foreach ($path AS $x => $crumb) {
  31.  // Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
  32.     $title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));
  33.  
  34.     // If we are not on the last index, then display an <a> tag
  35.     if ($x != $last)
  36.         $breadcrumbs[] = "<a href="$base$crumb">$title</a>";
  37.     // Otherwise, just display the title (minus)
  38.     else
  39.         $breadcrumbs[] = $title;
  40. }
  41.  
  42. // Build our temporary array (pieces of bread) into one big string :)
  43. return implode($separator, $breadcrumbs);
  44. }
  45.  
  46. ?>
  47.  
  48. <p><?= breadcrumbs() ?></p>
  49.        
  50. $upToNowCrumbs = array();
  51. // Build the rest of the breadcrumbs
  52. foreach ($path as $x => $crumb) {
  53.     $upToNowCrumbs[] = $crumb;
  54.     // Our "title" is the text that will be displayed (strip out .php and turn '_' into a space)
  55.     $title = ucwords(str_replace(Array('.php', '_'), Array('', ' '), $crumb));
  56.  
  57.     // If we are not on the last index, then display an <a> tag
  58.     if ($x != $last)
  59.         $breadcrumbs[] = "<a href="$base".implode('/', $upToNowCrumbs)."">$title</a>";
  60.     // Otherwise, just display the title (minus)
  61.     else
  62.         $breadcrumbs[] = $title;
  63. }