[ Index ]
 

Code source de SPIP Agora 1.4

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/Pear/DB/docs/ -> TreeMenu_example.php (source)

   1  <?php /** $Id: TreeMenu_example.php,v 1.5 2003/12/30 17:07:37 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://root:3ugr4ts@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      'options' => array('icon' => 'folder.gif', 'expandedIcon' => 'folder-expanded.gif'),
 108  );
 109  
 110  $output =& DB_NestedSet_Output::factory($params, 'TreeMenu');
 111  // Create the javascript menu.
 112  // You'll need to copy over the images directory and TreeMenu.js file that
 113  // comes with HTML_TreeMenu into a web-accessible directory.
 114  // Set the location of the images for the menu here
 115  echo '<script src="TreeMenu.js" language="JavaScript" type="text/javascript"></script>';
 116  $output->setOptions('printTree', array('images' => '/imagesAlt2'));
 117  $output->printTree();
 118  
 119  // Create the select list menu.
 120  $output->printListbox();
 121  
 122  // }}}
 123  ?>
 124  </div>
 125  </body>
 126  </html>


Généré le : Sat Feb 24 14:40:03 2007 par Balluche grâce à PHPXref 0.7