PHPBuilder - Navigation Tree



RSS Twitter
Snippets Algorithms

Navigation Tree

by: Lee Aholima
|
December 4, 2002

Version: 1.0

Type: Class

Category: Algorithms

License: GNU General Public License

Description: Navigation Tree which uses Mysql to store as many trees as you like. Has two main files, treeclass.inc and tree.php. Can have as many levels in the tree as physical limitations can handle. Uses a recursive function to prepare the tree to display.



<?php
/*
**	Company:	Day Five Consulting Ltd
**	Date Created:	20021203
**	Author:		Lee Aholima
**	File:		treeclass.inc
**	Function:
**		Creates a directory tree based on information
**		held in a Mysql database.
**		There must be a table in the database that
**		has the following columns
**
**		tree varchar(50), for tree name - all nodes in the tree have the same tree name
**		parent varchar(30), codename of parent
**		rootbranchorleaf char(1), 'R' if root, 'B' if branch, 'L' if leaf
**		codename varchar(30), uniquely identifies root, branch or leaf
**		displayname varchar(255), what is displayed on the tree
**		page varchar(255), webpage that is to be displayed as content.
*/

class myTree 
{

var $tree;
var $currentRootBranchOrLeaf;
var $mysql_desc;
var $counter;
var $tmpTree;
var $maxLevel;

function myTree() {
	$this->tree = array();
	$this->mysql_desc = null;
	$this->counter = 0;
	$this->tmpTree = array();
	$this->maxLevel = 0;
}

function dbConnect($mysqlhost,$database,$user_id,$user_pwd) {
	$this->mysql_desc=mysql_connect($mysqlhost,$user_id,$user_pwd) or die("Could not connect to Database Server");
	mysql_select_db($database,$this->mysql_desc) or die ("Could not select Database");
}

function initTree ($treeName) {

	/* get root */
	$sq="SELECT '-1' as indexofparent,'N' as expanded,parent,rootbranchorleaf,codename,displayname,page,'-1' as level,'1' as display FROM tree where tree='".$treeName."' and rootbranchorleaf='R'";
	$result=mysql_query($sq,$this->mysql_desc);
	$i=0;
	while ($row=mysql_fetch_object($result))
	{
		$this->tmpTree[$i++] = $row;
		/*echo "<font color=black>".$tmpTree[$i-1]->codename."</font><br>";*/
	}
	mysql_free_result($result);

	/* get leaves and branches */
	$sq="SELECT '-1' as indexofparent,'N' as expanded,parent,rootbranchorleaf,codename,displayname,page,'-1' as level,'0' as display FROM tree where tree='".$treeName."' and rootbranchorleaf in ('B','L') order by displayname";
	$result=mysql_query($sq,$this->mysql_desc);
	while ($row=mysql_fetch_object($result))
	{
		$this->tmpTree[$i++] = $row;
		/*echo "<font color=black>".$tmpTree[$i-1]->codename."</font><br>";*/
	}
	mysql_free_result($result);

	/* set the parent index of the tree to be the parent of branch or leaf */
	$j=0;
	while ($j < sizeof($tmpTree)) {
		$parent=$this->tmpTree[$j]->parent;
		for ($k=0;$k < sizeof($this->tmpTree); $k++) {
			if ($this->tmpTree[$k]->codename == $parent) {
				$this->tmpTree[$j]->indexofparent = $k;
			}
		}
		/*echo "<font color=black>".$j.",".$tmpTree[$j]->codename.",".$tmpTree[$j]->indexofparent."</font><br>";*/
		$j++;
	}

	$this->counter=0;
	$this->maxLevel = 0;
	$this->currentRootBranchOrLeaf = 0;
	$this->insertIntoTree(0,0);
}

function insertIntoTree($RBoL,$level) {
	$newLevel=false;
	$this->tmpTree[$RBoL]->level = $level;
	$this->tree[$this->counter][0] = $this->tmpTree[$RBoL]->indexofparent;
	$this->tree[$this->counter][1] = $this->tmpTree[$RBoL]->expanded;
	$this->tree[$this->counter][2] = $this->tmpTree[$RBoL]->parent;
	$this->tree[$this->counter][3] = $this->tmpTree[$RBoL]->rootbranchorleaf;
	$this->tree[$this->counter][4] = $this->tmpTree[$RBoL]->codename;
	$this->tree[$this->counter][5] = $this->tmpTree[$RBoL]->displayname;
	$this->tree[$this->counter][6] = $this->tmpTree[$RBoL]->page;
	$this->tree[$this->counter][7] = $this->tmpTree[$RBoL]->level;
	$this->tree[$this->counter][8] = $this->tmpTree[$RBoL]->display;
	$this->counter++;
	for ($k=1;$k < sizeof($this->tmpTree); $k++) {
		if ($this->tmpTree[$k]->parent == $this->tmpTree[$RBoL]->codename) {
			$newLevel=true;
			$this->insertIntoTree($k,$level + 1);
		}
	}
	if ($newLevel == true)
	{
		if ($level > $this->maxLevel) $this->maxLevel = $level;
	}
}
};
?>
-------------------------------------------------

<?php
session_start();
require("treeclass.inc");
function displayTree (&$tree,$currentRootBranchOrLeaf,$action) {
	$j = $currentRootBranchOrLeaf;
	if ($action == "c") {
		$currentLevel = $tree[$j][7];
		$tree[$j][1] = "N";
		$j++;
		while ($j < sizeof($tree)) {
			if ($tree[$j][7] > $currentLevel) {
				$tree[$j][8] = "0";
				$tree[$j][1] = "N";
				$j++;
			}
			else {
				break;
			}
		}
	}
	else if ($action == "e") {
		$currentLevel = $tree[$j][7];
		$parent = $tree[$currentRootBranchOrLeaf][4];
		$tree[$j][1] = "Y";
		$j++;
		while ($j < sizeof($tree)) {
			if (($tree[$j][7] == $currentLevel + 1) && $parent == $tree[$j][2]) {
				$tree[$j][8] = "1";
			}
			$j++;
		}
	}

	echo "<table align=left width=230>";
	for ($k=0 ;$k < sizeof($tree) ; $k++) {
		if ($tree[$k][8] == "1") {
			$currentColor = ($k==$currentRootBranchOrLeaf ? "color=red" : "");
			$img = $tree[$k][3] == "L" ? "leaf.gif" : ($tree[$k][1]=="Y" ? "minus.gif" : "plus.gif");
			$tmpAction = $tree[$k][3] == "L" ? "n" : ($tree[$k][1]=="Y" ? "c" : "e");
			echo "<tr><td width=230 colspan=1 ".$currentColor.">";
			for ($l=0;$l < $tree[$k][7];$l++) {
				echo "&nbsp;&nbsp;&nbsp;";
			}
			echo "<a href=\"tree.php?crbol=".$k."&amp;action=".$tmpAction."\"><img border=0 src=\"".$img."\">&nbsp;<a href=\"tree.php?crbol=".$k."&amp;action=n\">".$tree[$k][5]."</td>";
			echo "</tr>";
		}
	}
	echo "</table>";
	return ($tree[$currentRootBranchOrLeaf][6]);
}

?>
<html>
<head>
<title>Hello</title>
</head>
<body bgcolor=grey>
<?php
if (!session_is_registered("tree")) {
	$ltmTree = new myTree;
	$ltmTree->dbConnect("localhost:3306","tampam","root","");
	$ltmTree->initTree(urldecode($treeName));
	$tree = $ltmTree->tree;
	$maxlevel = $ltmTree->maxLevel;
	session_register("tree");
	session_register("maxlevel");
}
$theHref = displayTree($tree,$crbol,$action);
echo "</body>";
echo "</html>";
echo "<script language=\"Javascript\">";
echo "parent.frames[\"frame1\"].location.href = \"".$theHref."\";";
echo "</script>";
?>

-------------------------------------------------------------------------

An example of the top/parent script...

<?php
$crbol = "0";
$action = "n";
$treeName = "lookuptypemaintenance";
?>
<html>
<head>
<title>Time And Materials</title>
<meta name="author" content="Day Five Consulting Ltd, Upper Hutt, New Zealand">
</head>
<frameset cols="230,*" border=0>
	<frame src=<?php echo "\"tree.php?crbol=".$crbol."&amp;action=".$action."&amp;treeName=".urlencode($treeName)."\"";?> name="tree">
	<frame src=<?php echo "\"blankpage.php\"";?> name="frame1">
</frameset>
</html>


--------------------------------------------------------
the table which stores the trees and some sample data. Note you need to create some .php files as
named in the page column of the sample data.

# phpMyAdmin MySQL-Dump
# version 2.2.2
# http://phpwizard.net/phpMyAdmin/
# http://phpmyadmin.sourceforge.net/ (download page)
#
# Host: localhost
# Generation Time: Dec 04, 2002 at 08:23 PM
# Server version: 3.23.49
# PHP Version: 4.0.6
# Database : `tampam`
# --------------------------------------------------------

#
# Table structure for table `tree`
#

CREATE TABLE tree (
  tree varchar(50) NOT NULL default '',
  parent varchar(30) default NULL,
  codename varchar(30) NOT NULL default '',
  rootbranchorleaf char(1) NOT NULL default '',
  displayname varchar(255) NOT NULL default '',
  page varchar(255) NOT NULL default '',
  PRIMARY KEY  (codename)
) TYPE=MyISAM;

#
# Dumping data for table `tree`
#

INSERT INTO tree VALUES ('lookuptypemaintenance', NULL, 'lookuptypemaintenance', 'R', 'Lookup Type Maintenance', 'lookuptypesmain.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'lookuptypemaintenance', 'tasktype', 'B', 'Task Type', 'tasktype.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'tasktype', 'addtasktype', 'L', 'Add Task Type', 'addtasktype.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'tasktype', 'updatetasktype', 'B', 'Update Task Type', 'updatetasktype.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'updatetasktype', 'test3rdlevelA', 'L', 'A. Test 3rd Level', 'test3rdlevela.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'updatetasktype', 'test3rdlevelB', 'L', 'B. Test 3rd Level', 'test3rdlevelb.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'lookuptypemaintenance', 'usertype', 'B', 'User Type', 'usertype.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'usertype', 'addusertype', 'L', 'Add User Type', 'addusertype.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'usertype', 'updateusertype', 'L', 'Update User Type', 'updateusertype.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'lookuptypemaintenance', 'roletype', 'B', 'Role Type', 'roletype.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'roletype', 'addroletype', 'L', 'Add Role Type', 'addroletype.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'roletype', 'updateroletype', 'L', 'Update Role Type', 'updateroletype.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'lookuptypemaintenance', 'defecttype', 'B', 'Defect Type', 'defecttype.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'defecttype', 'adddefecttype', 'L', 'Add Defect Type', 'adddefecttype.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'defecttype', 'updatedefecttype', 'L', 'Update Defect Type', 'updatedefecttype.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'lookuptypemaintenance', 'defectstatustype', 'B', 'Defect Status Type', 'defectstatustype.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'defectstatustype', 'adddefectstatustype', 'L', 'Add Defect Status Type', 'adddefectstatustype.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'defectstatustype', 'updatedefectstatustype', 'L', 'Update Defect Status Type', 'updatedefectstatustype.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'lookuptypemaintenance', 'defectstatuslink', 'B', 'Defect Status Link', 'defectstatuslink.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'defectstatuslink', 'adddefectstatuslink', 'L', 'Add Defect Status Link', 'adddefectstatuslink.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'defectstatuslink', 'updatedefectstatuslink', 'L', 'Update Defect Status Link', 'updatedefectstatuslink.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'lookuptypemaintenance', 'hourlyrate', 'B', 'Hourly Rate', 'hourlyrate.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'hourlyrate', 'addhourlyrate', 'L', 'Add Hourly Rate', 'addhourlyrate.php');
INSERT INTO tree VALUES ('lookuptypemaintenance', 'hourlyrate', 'updatehourlyrate', 'L', 'Update Hourly Rate', 'updatehourlyrate.php');

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Lee Aholima

Comment:



Comment:

(Maximum characters: 1200). You have characters left.