Html_Menu with Nested Set
| From: | Pete | Date: | Tue, 25 May 2004 14:04:01 +0000 |
| Subject: | Html_Menu with Nested Set | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-12728@lists.php.net to get a copy of this message | ||
Hi,
I just can not get it right. Here is my menu.php
<?php
require_once('HTML/Menu.php');
require_once('DB/NestedSet.php');
require_once('DB/NestedSet/Output.php');
$dsn = 'mysql://root:test@localhost/test';
$params = array(
'id' => 'id',
'parent_id' => 'rootid',
'left_id' => 'l',
'right_id' => 'r',
'order_num' => 'norder',
'level' => 'level',
'id_trans' => 'id_trans',
'url' => 'url',
'name' => 'name',
);
$nestedSet =& DB_NestedSet::factory('DB', $dsn, $params);
// we want the nodes to be displayed ordered by name, so we add the secondarySort attribute
$nestedSet->setAttr(array(
'node_table' => 'nested_set',
'lock_table' => 'nested_set_locks',
'secondarySort' => 'name',
)
);
// get data (important to fetch it as an array, using the true flag)
$data = $nestedSet->getAllNodes(true);
// }}}
// {{{ manipulate data
// add links to each item
foreach ($data as $id => $node) {
$data[$id]['url'] =
$data[$id]['url'].'?nodeID='.$node['id'];
}
// }}}
// {{{ render output
$params = array(
'structure' => $data,
'titleField' => 'name',
'urlField' => 'url');
// Create the output driver object
$output =& DB_NestedSet_Output::factory($params, 'Menu');
// Fetch the menu array
$structure = $output->returnStructure();
// Instantiate the menu object, we presume that $data contains menu structure
$currentUrl = $_SERVER['PHP_SELF'].'?nodeID='.$_GET['nodeID'];
?>
I made an extra column url in the database and assigned the urls like this:
foreach ($data as $id => $node) {
$data[$id]['url'] =
$data[$id]['url'].'?nodeID='.$node['id'];
}
After I have done that the menu doesn't expand anymore.
I include the above script in for example news.php
include('menu.php');
$menu = & new HTML_Menu($structure, 'sitemap');
// Force menu to understand the nodeID passed with the request
$menu->forceCurrentUrl($currentUrl);
$menu->setMenuType('urhere');
$menu_urhere = str_replace('border="1"', 'border="0"',
$menu->get());
$page->assign('menu_urhere', $menu_urhere);
$menu->setMenuType('tree');
$menu_tree = str_replace('border="1"', 'border="0"',
$menu->get());
$page->assign('menu_tree', $menu_tree);
How can I assign the urls from the database properly ?
Thank you.