home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / TreeMenu_example.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  3.5 KB  |  126 lines

  1. <?php /** $Id: TreeMenu_example.php,v 1.4 2003/07/18 18:52:10 jrust Exp $ */ ?>
  2. <html>
  3.   <title>DB_NestedSet using TreeMenu Output class</title>
  4. <body>
  5. <div style="font-weight: bold;">DB_NestedSet using the TreeMenu Output class</div>
  6. <div>
  7. <?php
  8. /**
  9.  * Tests the DB_NestedSet class using the TreeMenu renderer
  10.  * Requires that you have HTML_TreeMenu installed
  11.  *
  12.  * @author Jason Rust <jrust@rustyparts.com>
  13.  */
  14. // {{{ mysql dump
  15.  
  16. /**
  17.  * Dump of the example mysql table and data:
  18. #
  19. # Table structure for table `nested_set`
  20. #
  21.  
  22. CREATE TABLE `nested_set` (
  23.   `id` int(10) unsigned NOT NULL default '0',
  24.   `parent_id` int(10) unsigned NOT NULL default '0',
  25.   `order_num` tinyint(4) unsigned NOT NULL default '0',
  26.   `level` int(10) unsigned NOT NULL default '0',
  27.   `left_id` int(10) unsigned NOT NULL default '0',
  28.   `right_id` int(10) unsigned NOT NULL default '0',
  29.   `name` varchar(60) NOT NULL default '',
  30.   PRIMARY KEY  (`id`),
  31.   KEY `right` (`right_id`),
  32.   KEY `left` (`left_id`),
  33.   KEY `order` (`order_num`),
  34.   KEY `level` (`level`),
  35.   KEY `parent_id` (`parent_id`),
  36.   KEY `right_left` (`id`,`parent_id`,`left_id`,`right_id`)
  37. ) TYPE=MyISAM;
  38.  
  39. #
  40. # Dumping data for table `nested_set`
  41. #
  42.  
  43. INSERT INTO `nested_set` VALUES (5, 5, 1, 1, 1, 10, 'Root A');
  44. INSERT INTO `nested_set` VALUES (7, 7, 1, 1, 1, 4, 'Root B');
  45. INSERT INTO `nested_set` VALUES (6, 5, 1, 2, 2, 5, 'Sub1 of A');
  46. INSERT INTO `nested_set` VALUES (1, 5, 2, 2, 6, 9, 'Sub2 of A');
  47. INSERT INTO `nested_set` VALUES (2, 5, 1, 3, 3, 4, 'Child of Sub1');
  48. INSERT INTO `nested_set` VALUES (3, 5, 1, 3, 7, 8, 'Child of Sub2');
  49. INSERT INTO `nested_set` VALUES (4, 7, 1, 2, 2, 3, 'Sub of B');
  50. # --------------------------------------------------------
  51.  
  52. #
  53. # Table structure for table `nested_set_locks`
  54. #
  55.  
  56. CREATE TABLE `nested_set_locks` (
  57.   `lockID` char(32) NOT NULL default '',
  58.   `lockTable` char(32) NOT NULL default '',
  59.   `lockStamp` int(11) NOT NULL default '0',
  60.   PRIMARY KEY  (`lockID`,`lockTable`)
  61. ) TYPE=MyISAM COMMENT='Table locks for comments';
  62.  
  63. */
  64.  
  65. // }}}
  66. // {{{ set up variables
  67.  
  68. require_once('DB/NestedSet.php');
  69. require_once('DB/NestedSet/Output.php');
  70. $dsn = 'mysql://user:pass@localhost/test';
  71. $params = array(
  72.     'id'        => 'id',
  73.     'parent_id' => 'rootid',
  74.     'left_id'   => 'l',
  75.     'right_id'  => 'r',
  76.     'order_num' => 'norder',
  77.     'level'     => 'level',
  78.     'name'      => 'name', 
  79. );
  80.  
  81. $nestedSet =& DB_NestedSet::factory('DB', $dsn, $params); 
  82. // we want the nodes to be displayed ordered by name, so we add the secondarySort attribute
  83. $nestedSet->setAttr(array(
  84.         'node_table' => 'nested_set', 
  85.         'lock_table' => 'nested_set_locks', 
  86.         'secondarySort' => 'name',
  87.     )
  88. );
  89. // get data (important to fetch it as an array, using the true flag)
  90. $data = $nestedSet->getAllNodes(true);
  91.  
  92. // }}}
  93. // {{{ manipulate data
  94.  
  95. // add links to each item
  96. foreach ($data as $id => $node) {
  97.      $data[$id]['link'] = 'http://example.com/foo.php?' . $node['id'];
  98. }
  99.  
  100. // }}}
  101. // {{{ render output
  102.  
  103. $params = array(
  104.     'structure' => $data,
  105.     'textField' => 'name',
  106.     'linkField' => 'link',
  107. );
  108.  
  109. $output =& DB_NestedSet_Output::factory($params, 'TreeMenu');
  110. // Create the javascript menu.
  111. // You'll need to copy over the images directory and TreeMenu.js file that
  112. // comes with HTML_TreeMenu into a web-accessible directory.
  113. // Set the location of the images for the menu here
  114. echo '<script src="TreeMenu.js" language="JavaScript" type="text/javascript"></script>';
  115. $output->setOptions('printTree', array('images' => '/imagesAlt2'));
  116. $output->printTree();
  117.  
  118. // Create the select list menu.
  119. $output->printListbox();
  120.  
  121. // }}}
  122. ?>
  123. </div>
  124. </body>
  125. </html>
  126.