I was searching for ages for a script that used a mysql database to store information for breadcrumbs. I eventually stumbled across this script found on this site.
http://www.phpbuilder.com/board/showthread.php?t=10247839
I have modified the code slightly and put it into a class / function, for the use in my site (hope nobody minds if i do) and hopefully make it easier for people to port into their own devlopments.
I am putting up the modified code in here hoping that it will be a help to others.
<?php
/*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// PHP-Mysql Login Based BreadCrumber\\
// Author: Jake Cantrell \\
// jcantrell [at] remove-me.mygov.us \\
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
/*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// Modified: Febuary 2009 \\
// Author: Jason Mitchell \\
// mitchell.jasonj [at] gmail.remove-me.com \\
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
** View original at http://www.phpbuilder.com/board/archive/index.php/t-10247839.html **
Database settings are stored in a settings file.
Requires you to parse a page title to the function.
use the following code as a basic test of the class:
<?php
require($_SERVER['DOCUMENT_ROOT']."crumbs.php");
$title = "test";
$result = $crumbs->dispcrumbs($title);
echo "<br/ > did it work? <br /><br />
$result";
?>
this assumes the srcipt below is saved as crumbs.php
*/
//Site Settings
require_once($_SERVER['DOCUMENT_ROOT']."/includes/lib/settings.php");
Class dbcrumbs{
var $connection;
Function dbcrumbs(){
$this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());
}
Function dispcrumbs($title){
$timeoutseconds = 1500; // 24 Minutes, for session timeouts
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;
$return = "";
$error ="";
// Delete old values
$temp = "DELETE FROM crumbs WHERE timestamp < $timeout AND timestamp != 0";
$delete = mysql_query($temp, $this->connection);
if(!($delete)) {
$error .= "OLD VALUE DELETE FAILED";
return $error;
}
// Get the current page path. ex: /index.php
$page = parse_url($_SERVER["REQUEST_URI"]);
$current_page=$page[path];
$querystring = $_SERVER["QUERY_STRING"];
if ($current_page == "/ca_inspections.php") { $querystring = "type=$type"; }
if ($current_page == "/ca_lists.php") { $querystring = "type=$type"; }
// Delete all rows from the database for current user if the user is on a base page. The base pages are pulled from a database on our site, i removed the database calls because they would just cause more confusion.
if ($current_page == "/index.php" OR $current_page=="/" OR $current_page=="/logout.php"){
$queryd = "DELETE FROM crumbs WHERE (user = '$username')";
if (!mysql_query ($queryd, $this->connection) ){
die (mysql_error("didnt work"));
}
}
// // Select all rows from database for current user.
$ahh = mysql_query("SELECT * FROM crumbs WHERE user='$username'");
$num_rows = mysql_num_rows($ahh);
if ($num_rows > 0){
// If there are rows in the database, check to see if the current page exists in the database.
$temp = "SELECT * FROM crumbs WHERE user='$username' AND page = '$current_page' AND title='$title' ORDER BY id ASC LIMIT 0,1";
$ahh2 = mysql_query($temp, $this->connection);
$num_rows2 = mysql_num_rows($ahh2);
if ($num_rows2 > 0){
while ($d_row =mysql_fetch_array($ahh2)){
// Check to see if page title is the same for the current page as the link in the
// database. There are several places in our site where there are more than one
// page link in a single script. ex: Select Project Type, Edit Inspections -
// Select City User, Edit City User.
if ($d_row[title] == $title){
// If the current page exists in the database for the current user,
// delete all from the database where the level is higher than
// the current page's level.
$query = "DELETE FROM crumbs WHERE (level > '$d_row[level]')";
if (!mysql_query ($query, $this->connection) ){
die (mysql_error("didnt work"));
}
$query2 = "UPDATE crumbs SET querystring = '$querystring', timestamp = '$timestamp' WHERE id='$d_row[id]'";
if (!mysql_query ($query2, $this->connection) ){
die (mysql_error());
}
}
else{
while ($a_row =mysql_fetch_array($ahh)){
if ($curlevel < $a_row[level]){
$curlevel = $a_row[level];
}
$x++;
if ($x == $num_rows){
$curlevel = $curlevel + 1;
$query1 = "INSERT INTO crumbs (page, user, level, title, querystring, timestamp) values ('$current_page', '$username', '$curlevel', '$title', '$querystring', '$timestamp')";
if (!mysql_query ($query1, $this->connection) ){
die (mysql_error());
}
}
}
}
}
}
else{
// If the current page does not exist in the database,
// find the highest level of the users stored pages
// and insert the current page to the database with
// the next level.
while ($a_row =mysql_fetch_array($ahh)){
if ($curlevel < $a_row[level]){
$curlevel = $a_row[level];
}
$x++;
if ($x == $num_rows){
$curlevel = $curlevel + 1;
$query1 = "INSERT INTO crumbs (page, user, level, title, querystring, timestamp) values ('$current_page', '$username', '$curlevel', '$title', '$querystring', '$timestamp')";
if (!mysql_query ($query1, $this->connection) ){
die (mysql_error());
}
}
}
}
}
else{
// If there are no rows in the database (First entry to page, or have clicked on a base page)
// Insert the current base page into the database as level 1.
$level = 1;
$query2 = "INSERT INTO crumbs (page, user, level, title, querystring, timestamp) values ('$current_page', '$username', '$level', '$title', '$querystring', '$timestamp')";
if (!mysql_query ($query2, $this->connection) ){
die (mysql_error());
}
}
// Display BreadCrumb trail.
$temp = "SELECT * FROM crumbs WHERE user='$username' ORDER BY level";
$ahh3 = mysql_query($temp, $this->connection);
$num_rows3 = mysql_num_rows($ahh3);
while ($x_row =mysql_fetch_array($ahh3)){
$pagelink = $x_row[page];
$pagelink .= "?$x_row[querystring]";
$g++;
// If it's not the last level in the database, insert seperator.
if ($g < $num_rows3){
$return .= "<font face=\"Arial, Helvetica, sans-serif\" size=1 color=#cccccc><a href=\"http://".DOMAIN_NAME."$pagelink\">$x_row[title]</a></font>";
$return .= " > ";
}
else{
$return .= "<font face=\"Arial, Helvetica, sans-serif\" size=1 color=#000099><B><STRONG>$x_row[title]</STRONG></b></font>";
}
}
Return $return;
}
}
$crumbs = new dbcrumbs();
?>
Thanks for a great script.