Sorry it took me a few days to get to this. I've been on vacation. This was really built with our site in mind, but I think it is documented well enough that it could be modified to work with about any other site. Seems like I still have some issues with macs using this, but 99.9999% of our users will be using something other than mac.
This could also be made into a function pretty easily. I just didn't do it. I'm always open for comments/suggestions, so let me know what you think.
JAKE
MySQL Table
CREATE TABLE breadcrumb (
id int(50) NOT NULL auto_increment,
page varchar(255) NOT NULL default '',
querystring text NOT NULL,
title varchar(50) NOT NULL default '',
level varchar(50) NOT NULL default '',
user varchar(50) NOT NULL default '',
timestamp varchar(50) NOT NULL default '',
PRIMARY KEY (id)
) TYPE=MyISAM AUTO_INCREMENT=2999 ;
<?php
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// PHP-Mysql Login Based BreadCrumber
// Author: Jake Cantrell
// [email]jcantrell@mygov.us[/email]
//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// I have my database connect information in another file that is always included when this file is included.
// So you will need to insert your own connect statement.
// The title for the breadcrumb is declared just before the breadcrumb script is included. ex. $title = "Home";
// Client's Username is stored in a session on our site.
// Base Page (There are 4-5 different base pages on our site. The base page is actually determined in the header file.)
// in a normal site, the base could probably be the index/default page.
// Get Current Timestamp
$timeoutseconds = 1500; // 24 Minutes, for session timeouts
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;
// Delete old values
$delete = mysql_query("DELETE FROM breadcrumb WHERE timestamp<$timeout AND timestamp!='0'");
if(!($delete)) {
print "OLD VALUE DELETE FAILED";
}
// 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 breadcrumb WHERE (user = '$username')";
if (!mysql_query ($queryd, $link) )
{
die (mysql_error("didnt work"));
}
}
// Select all rows from database for current user.
$ahh = mysql_query("SELECT * FROM breadcrumb 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.
$ahh2 = mysql_query("SELECT * FROM breadcrumb WHERE user='$username' AND page = '$current_page' AND title='$title' ORDER BY id ASC LIMIT 0,1");
$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 breadcrumb WHERE (level > '$d_row[level]')";
if (!mysql_query ($query, $link) )
{
die (mysql_error("didnt work"));
}
$query2 = "UPDATE breadcrumb SET querystring = '$querystring', timestamp = '$timestamp' WHERE id='$d_row[id]'";
if (!mysql_query ($query2, $link) )
{
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 breadcrumb (page, user, level, title, querystring, timestamp) values ('$current_page', '$username', '$curlevel', '$title', '$querystring', '$timestamp')";
if (!mysql_query ($query1, $link) )
{
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 breadcrumb (page, user, level, title, querystring, timestamp) values ('$current_page', '$username', '$curlevel', '$title', '$querystring', '$timestamp')";
if (!mysql_query ($query1, $link) )
{
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 breadcrumb (page, user, level, title, querystring, timestamp) values ('$current_page', '$username', '$level', '$title', '$querystring', '$timestamp')";
if (!mysql_query ($query2, $link) )
{
die (mysql_error());
}
}
// Display BreadCrumb trail.
$ahh3 = mysql_query("SELECT * FROM breadcrumb WHERE user='$username' ORDER BY level");
$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)
{
print "<font face=\"Arial, Helvetica, sans-serif\" size=1 color=#cccccc><a href=\".$pagelink\">$x_row[title]</a></font>";
print " > ";
}
else
{
print "<font face=\"Arial, Helvetica, sans-serif\" size=1 color=#000099><B><STRONG>$x_row[title]</STRONG></b></font>";
}
}
?>