GeXus Posted April 22, 2007 Share Posted April 22, 2007 I'm using a tree traversal to store categories.. What I want to do is use the php to format how the categories are displayed.. For example, I'm getting the depth of the node, so a result set might look like this name depth General 1 Games 2 Action 3 Sales 4 Movies 2 Action 3 Drama 3 Sales 4 Based on this data, I want to be able to format it such as this.. <ul class="tree"> <li class="closed"><a href="#">Games</a> <ul> <li><a href="#">Action</a></li> <ul> <li><a href="#">Sales</a></li> </ul> </ul> </li> <li class="closed"><a href="#">Movies</a> <ul> <li><a href="#">Action</a></li> <li><a href="#">Drama</a> <ul> <li><a href="#">Sales</a></li> </ul> </li> </ul> </li> </ul> Ultimatly, this will be used for a typical folder tree layout... The problem is how in php do I use the depth value to echo out this format? Thanks a lot!! Link to comment https://forums.phpfreaks.com/topic/48185-format-tree-traversal/ Share on other sites More sharing options...
Barand Posted April 22, 2007 Share Posted April 22, 2007 If you set up an array of the categories so you have the category and its parent <?php $cats = array ( 1 => array ('General', 0), // category, parent 2 => array ('Games', 1), 3 => array ('Movies', 1), 4 => array ('Action', 2), 5 => array ('Action', 3), 6 => array ('Sales', 4), 7 => array ('Drama', 3), 8 => array ('Sales', 7), ); function listCats (&$cats, $parent) { $k = count($cats); for ($id=1; $id<=$k; $id++) { $arr = $cats[$id]; if ($arr[1] == $parent) { echo "<ul>\n"; echo "<li>{$arr[0]}</li>\n"; listCats($cats, $id); echo "</ul>\n"; } } } listCats($cats,0); ?> gives --> [pre] General Games Action Sales Movies Action Drama Sales [/pre] Link to comment https://forums.phpfreaks.com/topic/48185-format-tree-traversal/#findComment-235594 Share on other sites More sharing options...
GeXus Posted April 22, 2007 Author Share Posted April 22, 2007 How would I get the parent though.. since i'm using tree traversal? Link to comment https://forums.phpfreaks.com/topic/48185-format-tree-traversal/#findComment-235596 Share on other sites More sharing options...
GeXus Posted April 23, 2007 Author Share Posted April 23, 2007 Anyone have any ideas? Thanks! Link to comment https://forums.phpfreaks.com/topic/48185-format-tree-traversal/#findComment-235688 Share on other sites More sharing options...
Barand Posted April 23, 2007 Share Posted April 23, 2007 What data are you starting with? Link to comment https://forums.phpfreaks.com/topic/48185-format-tree-traversal/#findComment-235786 Share on other sites More sharing options...
GeXus Posted April 23, 2007 Author Share Posted April 23, 2007 Date? This doesnt use date.. it is simply a tree traversal.. Link to comment https://forums.phpfreaks.com/topic/48185-format-tree-traversal/#findComment-236176 Share on other sites More sharing options...
Barand Posted April 23, 2007 Share Posted April 23, 2007 Bye Link to comment https://forums.phpfreaks.com/topic/48185-format-tree-traversal/#findComment-236268 Share on other sites More sharing options...
GeXus Posted April 23, 2007 Author Share Posted April 23, 2007 Sorry? I don't think I understand what your talking about... It seemed the example you gave me was for an adjacency list model Link to comment https://forums.phpfreaks.com/topic/48185-format-tree-traversal/#findComment-236285 Share on other sites More sharing options...
Barand Posted April 23, 2007 Share Posted April 23, 2007 If you got the levels from your original data you can prob format it using that too. So I just wanted to know what it was, how it is formatted. As you were reluctant to provide requested information I out of here. Try http://www.visionexpress.com/en/index.php Link to comment https://forums.phpfreaks.com/topic/48185-format-tree-traversal/#findComment-236296 Share on other sites More sharing options...
GeXus Posted April 23, 2007 Author Share Posted April 23, 2007 I don't need glasses, thanks though! This is exactly what I have id, name, left, right 1, 'ELECTRONICS', 1, 20 2, 'TELEVISIONS', 2, 9 3, 'TUBE', 3, 4 4, 'LCD', 5, 6 5, 'PLASMA', 7, 8 6, 'PORTABLE ELECTRONICS', 10, 19 7, 'MP3 PLAYERS', 11, 14 8, 'FLASH', 12, 13 9, 'CD PLAYERS', 15, 16 10, '2 WAY RADIOS', 17, 18 There is nothing more I can give you! There are no dates associated with any of this. Link to comment https://forums.phpfreaks.com/topic/48185-format-tree-traversal/#findComment-236303 Share on other sites More sharing options...
Barand Posted April 23, 2007 Share Posted April 23, 2007 I don't need glasses, thanks though! Are you sure? I asked for starting data, not date Link to comment https://forums.phpfreaks.com/topic/48185-format-tree-traversal/#findComment-236315 Share on other sites More sharing options...
GeXus Posted April 23, 2007 Author Share Posted April 23, 2007 Bastard!! Ok.. maybe I do need glasses Anyways, I just figured it out.. was just a matter of using the level and displaying it based on equal, less than or greater then.. much simpler than I had though.. Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/48185-format-tree-traversal/#findComment-236349 Share on other sites More sharing options...
Barand Posted April 23, 2007 Share Posted April 23, 2007 try <?php $tree = array ( 1 => array ( 'node' => 'ELECTRONICS', 'left' => 1, 'right' => 20 ), array ( 'node' => 'TELEVISIONS', 'left' => 2, 'right' => 9 ), array ( 'node' => 'TUBE', 'left' => 3, 'right' => 4 ), array ( 'node' => 'LCD', 'left' => 5, 'right' => 6 ), array ( 'node' => 'PLASMA', 'left' => 7, 'right' => 8 ), array ( 'node' => 'PORTABLE ELECTRONICS', 'left' => 10, 'right' => 19 ), array ( 'node' => 'MP3 PLAYERS', 'left' => 11, 'right' => 14 ), array ( 'node' => 'FLASH', 'left' => 12, 'right' => 13 ), array ( 'node' => 'CD PLAYERS', 'left' => 15, 'right' => 16 ), array ( 'node' => '2 WAY RADIOS', 'left' => 17, 'right' => 18 ) ); function listNodes (&$tree, $id) { if (!isset($tree[$id]['listed'])) { echo "<ul>\n"; echo "<li>{$tree[$id]['node']}</li>\n"; $tree[$id]['listed'] = 1; // block further listing $k = count($tree); for ($nid=1; $nid <= $k; $nid++) { $nodedata = $tree[$nid]; if ($nodedata['left'] > $tree[$id]['left'] && $nodedata['right'] < $tree[$id]['right'] ) listNodes($tree, $nid); } echo "</ul>\n"; } } listNodes ($tree,1); ?> Link to comment https://forums.phpfreaks.com/topic/48185-format-tree-traversal/#findComment-236371 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.