For Programmers: Free Programming Magazines  


Home > Archive > PHP Pear > September 2007 > HTML Menu/DBNested - Only want to display tree with 2 levels









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author HTML Menu/DBNested - Only want to display tree with 2 levels
Mark Reynolds

2007-09-18, 8:02 am

I have an array which I'm trying to display as a 'Tree' menu via 'HTML
Menu' (example of the actual tree menu below, the array is below that). I've
come across a few problems. Firstly, as you would expect when the 'tree'
menu is displayed it displays three levels. Because I'm using the
DirectTreeRenderer the menu is displayed in a list and I'm trying to find a
way of just displaying 2 levels. Is this something I can limit as part of
HTML menu or is it something I have to put in place when creating the actual
array and if so, how do I do it? I've looked at
HTML_Menu_Renderer::finishLevel() - not sure if I'm along the right lines
there, but whatever it's supposed to do I can't get it to do it. Help
appreciated!

MENU'S HTML STRUCTURE:

<ul>
<li>Menu item 1
<ul>
<li>Menu item 1.1</li>
<li>Menu item 1.2
<ul>
<li>Menu item 1.2.1</li>
<li>Menu item 1.2.3</li>
</ul>
</li>
</ul>
</li>
<li>Menu item 2
<ul>
<li>Menu item 2.1</li>
<li>Menu item 2.2</li>
</ul>
</li>
</ul>


ARRAY

array(
1 => array(
'title' => 'Menu item 1',
'url' => '/item1.php',
'sub' => array(
11 => array('title' => 'Menu item 1.1', 'url' =>'/item1.1.php'),
12 => array('title' => 'Menu item 1.2', 'url' => '/item1.2.php',
'sub' => array(
121 => array('title' => 'Menu item 1.2.1', 'url'
=>'/item1.2.1.php'),
122 => array('title' => 'Menu item 1.2.2', 'url'
=>'/item1.2.2.php')
)
)
)
),
2 => array(
'title' => 'Menu item 2',
'url' => '/item2.php',
'sub' => array(
21 => array('title' => 'Menu item 2.1', 'url' =>'/item2.1.php'),
22 => array('title' => 'Menu item 2.2', 'url' => '/item2.2.php')
)
)
);


SCRIPT TO DISPLAY ARRAY AS TREE:

// Fetch the entire tree into an array
$data = $nestedSet->getAllNodes(true);

// Create the linksforeach ($data as $id =>$node) {
$data[$id]['url'] = $_SERVER['PHP_SELF'].'?nodeID=' . $node['id'] .
'&nodename=' . $node['name'];
}

// Send HTML_Menu the structure, title and URL for the items in our tree
$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();
$structure = & newHTML_Menu($structure);
$renderer =& new HTML_Menu_DirectTreeRenderer;

//Create the current URL and send it to HTML_Menu
$currentUrl = $_SERVER['PHP_SELF'].'?nodeID=' . $_GET['nodeID'] .
'&nodename=' .$_GET['nodename'];
$structure->forceCurrentUrl($currentUrl);
$structure->render($renderer,'tree');

// Output the menu
echo '<div id="mainleftnav" class="leftbox">'.$renderer->toHtml()."</div>";
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com