<?php
include('visitation.adodb.class.php');
/*
#
# Table structure for table `tree`
#
CREATE TABLE `tree` (
`id` int(11) NOT NULL auto_increment,
`parent` int(11) NOT NULL default '1',
`title` varchar(20) NOT NULL,
`left` int(11) NOT NULL,
`right` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `ParentId` (`parent`,`left`,`right`)
) ENGINE=MyISAM AUTO_INCREMENT=10 ;
#
# Dumping data for table `tree`
#
INSERT INTO `tree` VALUES (1, 1, 'Food', 1, 18);
INSERT INTO `tree` VALUES (2, 1, 'Meat', 2, 7);
INSERT INTO `tree` VALUES (3, 2, 'Beaf', 3, 4);
INSERT INTO `tree` VALUES (4, 2, 'Fish', 5, 6);
INSERT INTO `tree` VALUES (5, 1, 'Fruit', 8, 17);
INSERT INTO `tree` VALUES (6, 5, 'Red', 9, 12);
INSERT INTO `tree` VALUES (7, 6, 'Apple', 10, 11);
INSERT INTO `tree` VALUES (8, 5, 'Yellow', 13, 16);
INSERT INTO `tree` VALUES (9, 8, 'Banana', 14, 15);
*/
// include the ADODB library
include("ADOdb/adodb.inc.php");
// create an object instance
// configure library for a MySQL connection
$db = NewADOConnection("mysql");
// open connection to database
$db->Connect("localhost", "root", "", "test") or die("Unable to connect!");
// get resultset as associative array
$ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;
// Get and object of Visitation class and setup properties value
$tree = new Visitation();
$tree->setTreeTable('tree');
$tree->setLeftBranch('left');
$tree->setRightBranch('right');
$tree->setLeafID('id');
$tree->setLeafParent('parent');
// This method should be requested whenever you ADD or DELETE any node in your tree table
$tree->updateTree($db);
echo '<pre>
Example "tree" table contents:
Food (1)
|
|___Meat (2)
| |
| |___Beaf (3)
| |
| |___Fish (4)
|
|___Fruit (5)
|
|___Red (6)
| |
| |__Apple (7)
|
|___Yellow (8)
|
|__Banana (9)
';
echo "\nFind out leaf nodes in current tree (nodes have no child):\n";
print_r($tree->findLeaf($db));
echo "\nFind out members of sub tree for given node (root node id is 5 here):\n";
print_r($tree->findMembers($db, 5));
echo "\nFind out context (the path to a specific node) of given node (this example for node id 9):\n";
print_r($tree->fetchContext($db, 9));
echo "\nFind out level/deepth of given node (this example for node id 6):\n";
print_r($tree->findLevel($db, 6));
echo "\n\nFind out how many descendants given node has (this example for node id 5):\n";
print_r($tree->descendants($db, 5));
echo '</pre>';
?>
|