PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Khaled Al-Shamaa   Celko Visitation Model   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Celko Visitation Model
Manipulate hierarchic records in a MySQL database
Author: By
Last change:
Date: 18 years ago
Size: 2,394 bytes
 

Contents

Class file image Download
<?php
include('visitation.class.php');

/*
#
# Table structure for table `tree`
#

CREATE TABLE `tree` (
  `NodeId` int(11) NOT NULL auto_increment,
  `ParentId` int(11) NOT NULL default '1',
  `NodeName` varchar(20) NOT NULL,
  `Left` int(11) NOT NULL,
  `Right` int(11) NOT NULL,
  PRIMARY KEY (`NodeId`),
  KEY `ParentId` (`ParentId`,`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);

*/

// Don't forget to setup this configuration to connect your database where "tree" table is
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('test', $db);

// Get and object of Visitation class and setup properties value
$tree = new Visitation();
$tree->setTreeTable('tree');
$tree->setLeftBranch('Left');
$tree->setRightBranch('Right');
$tree->setLeafID('NodeId');
$tree->setLeafParent('ParentId');

// 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/depth 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>';

?>