Version: 1.02
Type: Class
Category: HTTP
License: GNU General Public License
Description: Breadcrumb navigation. Very easy to implement.
<?php
/*
File: bc.php
Version: 1.02
Desc: Simple class to create a 'breadcrumb' navigation menu
Author: Steve Spence sspence@po18.lcv.ne.jp
Date: 30/08/2004
Comments: See test pages for how to use to it.
Usage:
<?php
include('bc.php');
$title = "Top Page";
$bc = new Breadcrumb($title, 'dddd');
?>
<html>
<head>
<title>Top</title>
</head>
<body>
<?php $bc->dropBreadcrumbs($title); ?>
ETC.....
*/
class Breadcrumb {
var $bc_path;
function Breadcrumb($title, $bc_path='any_session_id1') {
$this->bc_path = $bc_path; // this is a session id so more than one breadcrumb top and path is possible
session_start();
if (!isset ($_SESSION[$this->bc_path]['title']) ) {
// setup session arrays
$_SESSION[$this->bc_path]['title'][0]= $title;
$_SESSION[$this->bc_path]['url'][0]= $this->full_name( $_SERVER['REQUEST_URI'] ) ;
$_SESSION['top'] = $title;
} else {
$this->setPage($title);
}
}
function full_name ($page) {
return "http://" . $_SERVER['SERVER_NAME'] . $page;
}
function getLinkWrap($url, $text) {
return "<A href=\"$url\">$text</A>";
}
function reset() {
unset($_SESSION[$this->bc_path]);
unset($_SESSION['top']);
}
function getIndex($needle, $haystack) {
if (in_array( $needle, $haystack)) {
for ($i=0;$i<count($haystack);$i++) {
if ( $needle == $haystack[$i] ) {
return $i;
}
}
}
return false;
}
function trimArray($last) {
// unset elements from and including $last
$ct= count($_SESSION[$this->bc_path]['title']);
for ($i=$last; $i<$ct; $i++) {
unset($_SESSION[$this->bc_path]['title'][$i]);
unset($_SESSION[$this->bc_path]['url'][$i]);
}
}
function setPage($title) {
// get index of this title, false means not in array
$i = $this->getIndex($title, $_SESSION[$this->bc_path]['title']);
if ($i === false) {
// not in array, so add it
$this->addPage($title);
}
else {
// in array, so trim all elements after it
$this->trimArray($i+1);
}
}
function addPage($title) {
// save title and complete get line ie. 'REQUEST_URI'
$_SESSION[$this->bc_path]['title'][]= $title;
$_SESSION[$this->bc_path]['url'][]= $this->full_name( $_SERVER['REQUEST_URI'] );
}
function dropBreadcrumbs($title) {
// print the bc's
print $this->getBreadcrumbs($title);
}
function getBreadcrumbs($title) {
session_start();
if ($_SESSION['top'] == $title) {
$s = $title;
$this->trimArray(1);
} else {
$a = $_SESSION[$this->bc_path]; // just a little shorthand
for ($i=0; $i<count($a['title'])-1; $i++) {
$s .= $this->getLinkWrap($a['url'][$i], $a['title'][$i]) . " >";
}
$s .= $title;
}
return $s;
}
}
?>