wilco Posted December 3, 2007 Share Posted December 3, 2007 How would I approach this problem? I want to build an unordered list from some database entries, but rebuilding the heirarchical information stored with each row. So here's some example rows: ID PARENT_ID NODE_PATH NAME 1 0 0000:0001 Administrators 2 0 0000:0002 Users 3 0 0000:0003 Guests 4 2 0000:0002:00004 Tom 5 2 0000:0002:00005 Dick 6 2 0000:0002:00006 Harry How might I go from the above to this: Administrators UsersTom Dick Harry [*]Guests I'm guessing some recursion will be in order, but I haven't had to think recursively since college and I'm a bit rusty. Any help appreciated! Quote Link to comment Share on other sites More sharing options...
revraz Posted December 3, 2007 Share Posted December 3, 2007 Hmm.. maybe explode your node path on : and then sort the array then output it? Not sure. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted December 3, 2007 Share Posted December 3, 2007 Is there any need to store the data in this format? Surely a simple 'account type' field would be more appropriate? I don't see this as being a complex tree stucture: surely there will be no deeper tree structure than as shown above? As in, there wont be a separate list under Tom, or under Dick etc. If you were to just store the account type, you could order by this, then by ID, and output the required list elements (Administrators, Users, Guests) when the account type changes. Would be a much easier approach. Quote Link to comment Share on other sites More sharing options...
wilco Posted December 3, 2007 Author Share Posted December 3, 2007 Actually, I just used that as a simplified example. The way it is actually used is for a heirarchical group structure (so pretend Tom, Dick, and Harry are sub-groups that could be parents for additional children). Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted December 3, 2007 Share Posted December 3, 2007 Fair enough. My preferred method for this sort of thing is a two dimensional array. The first element being the parent node. See this 'one i made earlier', which was for a nested menu: <?php function menu($menu,$index='Parent'){ echo '<ul>'; foreach($menu[$index] as $k => $v){ echo "<li><a href='$v'>$k</a></li>"; if(is_array($menu[$k])){ menu($menu,$k); } } echo '</ul>'; } $menu = array(); $menu["Parent"]["Home"] = "/index.php"; $menu["Parent"]["Contact Us"] = "/contactus.php"; $menu["Parent"]["Members"] = "/members/index.php"; $menu["Home"]["About"] = "/about.php"; $menu["Contact Us"]["Live Chat"] = "/chat/index.php"; $menu["Live Chat"]["Live Help"] = "/chat/connected.php"; $menu["Contact Us"]["Email"] = "/emailform.php"; menu($menu); ?> Obviously its not quite what you need, but if i were doing it i would be looking to transform the data into a similar array. Hope that helps. If you're still having problems tomorrow, ill post a better solution, but right now i'm off to bed. Quote Link to comment Share on other sites More sharing options...
wilco Posted December 4, 2007 Author Share Posted December 4, 2007 I finally sat down and worked my way through it yesterday, and came up with something very similar to what you suggested. In case it helps anyone else, here's what I came up with (and thanks for the help on this!): <?php /** * Builds a string containing an unordered list of groups. * @param integer $parentId the parent ID to start building the list from * @param array $groupVOs an array of group Value Objects from which to build the list * @return string the unordered list */ function buildGroupList($parentId, $groupVOs) { $result = ""; foreach($groupVOs as $groupVO) { if($groupVO->getParent_id() == $parentId) { $result.="<li>".$groupVO->getName()."</li>"; if(strlen($children = buildGroupList($groupVO->getId(), $groupVOs))) { $result.= $children; } } } if(strlen($result) > 0) { $result = "<ul>".$result."</ul>"; } return $result; } ?> Quote Link to comment Share on other sites More sharing options...
jcd Posted December 4, 2007 Share Posted December 4, 2007 I was curious, are you putting the NODE_PATH in the db manually, or generating them with code? Quote Link to comment Share on other sites More sharing options...
wilco Posted December 4, 2007 Author Share Posted December 4, 2007 It's generated with code when it's first inserted (or updated). I figured paying for the CPU/memory ahead of time would save me later when I needed to display the info. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.