Jump to content

[SOLVED] Making a <ul> tree . . .


wilco

Recommended Posts

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
  • Users
    • Tom
    • 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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.