I was working with a site a while back that had an odd menu setup where you could get to the same place more than one way, so I had to devise something that would keep a track of things for me.
The following class is what I came up with. It may not be the most elegant solution, but it should get the job done.
Code: Select all
<?php
class breadcrumb {
// Class Variables
private $intro = "You are here: "; // Configuration: Breadcrumb lead-in text
private $spacer = " > "; // Configuration: Breadcrumb delimiter
private $base_link = "index.php"; // Configuration: Base crumb URL
private $base_text = "Home"; // Configuration: Base crumb text
public $output; // Holds breadcrumb output string
public $this_page_name; // Holds the current levels text - for headings or whatever
public $this_page_link; // Holds the current levels url - for redirects to the same page - etc
public $back_link; // Holds the url for a link back one level
public function __construct($base_link = "index.php", $base_text = "Home") {
if(!isset($_SESSION['BC_NAV'])) $_SESSION['BC_NAV'][$base_text] = $base_link;
}
public function add_level($new_link, $new_text, $parent_link = false) {
if(!isset($_SESSION['BC_NAV'][$new_text])) {
// Add a new child to the breadcrumb tree
$_SESSION['BC_NAV'][$new_text] = $new_link;
} else {
// Clip any items after the current one
while(end($_SESSION['BC_NAV']) != $new_link) array_pop($_SESSION['BC_NAV']);
}
// If $parent_link is set, make sure the new level is a direct descendant or shortcurcuit to base crumb
if($parent_link) {
$parent_found = false;
foreach($_SESSION['BC_NAV'] as $crumb_text=>$crumb_link) {
if($parent_found) { if($crumb_link != $new_link) unset($_SESSION['BC_NAV'][$crumb_text]); }
if($crumb_link == $parent_link) $parent_found = true;
}
}
$this->generate();
}
private function generate() {
$final_element = end($_SESSION['BC_NAV']);
foreach($_SESSION['BC_NAV'] as $crumb_text=>$crumb_link) {
if($crumb_link==$final_element) {
$new_bc[] = "<em>$crumb_text</em>";
$this->this_page_name = $crumb_text;
$this->this_page_link = $crumb_link;
} else {
$this->back_link = $crumb_link;
$new_bc[] = "<a href=\"$crumb_link\">$crumb_text</a>";
}
}
$this->output = $this->intro . implode($this->spacer, $new_bc);
}
}
?>
Now to be honest with you, I don't know if this is the latest revision or one of the versions on the way to getting it the way I wanted it, but after a brief scan it seems like it should be OK. Nothing jumps out at me.
Anyhow, you'd use it like this.
Code: Select all
// Start breadcrumb tracking
require_once(PATH_CLASS . "class_breadcrumb.php");
$bread = new breadcrumb();
// Add breadcrumb level
$bread->add_level("index.php", "Home");
// Display page details
<div id="page-name"><?php echo $bread->this_page_name; ?></div>
<div id="page-bread"><?php echo $bread->output; ?></div>
You don't have to use the div's for display purposes of course ... it's just how I did it so that I could apply the styles as I liked.
So basically you just the above on each page, except change the $bread->add_level() accordingly. E.g ...
Code: Select all
$bread->add_level("signup.php", "Join Now");
Anyhow, they should keep stacking on top of each other until you start moving back into previous pages, were it should just clip the breadcrumb down accordingly.
As I have said .. it may or may not work 100% ... but it should help out if you don't have any other logic structure to tie the breadcrumbs to.
Cheers