PHP Breadcrumb Navigation

Posted on 2009-12-22
Last Modified: 2012-08-14
Is there an alternative 'simple' way to create the navigation trail below?  I'm having user permission problems with creating temporary tables.

The table structure is
nodeId, parentId, title

$id = $_REQUEST['id']; 

//Breadcrumb function
	function BreadcrumbRecordset($table, $id, $fk, $name, $get, $db, $conn) {
		mysql_select_db($db, $conn);
		// drop if exists
		$sql = "DROP TABLE IF EXISTS tmp_tbl";
		mysql_query($sql, $conn);

		// create tmp table
		$sql = "create temporary table tmp_tbl (KT_id int(11), $id VARCHAR(255), $name VARCHAR(255)) type=heap";
		mysql_query($sql, $conn) or die(mysql_error());
		// insert
		$getid = @$_GET[$get];
		$tmpid = 1;
		while($getid) {
			if (!is_numeric($getid)) {
			$sql = "select $id, $fk, $name from $table where $id=$getid";
			$rs = mysql_query($sql, $conn) or die(mysql_error());
			$row_rs = mysql_fetch_assoc($rs) or die(mysql_error());
			if ($row_rs) {
				$nname = $row_rs[$name];
				$sql = "insert into tmp_tbl (KT_id, $id, $name) values ($tmpid, $getid, '$nname')";
				mysql_query($sql, $conn) or die(mysql_error());

		$sql = "select * from tmp_tbl order by KT_id desc";
		$ret = mysql_query($sql, $conn) or die(mysql_error());
		return $ret;

$Breadcrumbs = BreadcrumbRecordset('tbl_Menu','nodeId','parentId','title','id',$database,$server);
$row_Breadcrumbs = mysql_fetch_assoc($Breadcrumbs);
$totalRows_Breadcrumbs = mysql_num_rows($Breadcrumbs);


<!-- display breadcrumb trail -->
     <div id="breadcrumbs">
	   <a href="index.php?id=1" title="Home">Home</a>
          <?php do { ?>
          <?php if ($totalRows_Breadcrumbs > 0) { ?>
            &gt; <a href="index.php?id=<?php echo $row_Breadcrumbs['nodeId']; ?>" title="<?php echo $row_Breadcrumbs['title']; ?>"><?php echo $row_Breadcrumbs['title']; ?></a>
            <?php } ?>
          <?php } while ($row_Breadcrumbs = mysql_fetch_assoc($Breadcrumbs)); ?>
	   </div>The fo

Open in new window

Question by:studio1090
    1 Comment
    LVL 107

    Accepted Solution

    Looks like maybe your code snippet was cut off?  Anyway, one strategy for a breadcrumb navigation trail depends on having permanent tables.  The reason for this is that the temporary tables are lost when a script ends, so they are not very useful from page to page.

    You can use GRANT to grant privileges for creating temporary tables.  See

    Your hosting service should set this up for you in a jiffy.

    The strategy I would follow for breadcrumbs would go something like this...

    On every page do session_start()
    record the timestamp, full URL and the session_id() in your DB table

    The full URL is available by combining the protocol - check $_SERVER["SERVER_PROTOCOL"] - with the host and URI strings

    To show the path through the web site, SELECT from the table WHERE the session_id matches ORDER BY auto_increment key DESC.

    To clean up this table and keep it from growing out of hand, DELETE FROM the table where the timestamp is older than a day or so.

    HTH, ~Ray

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now