Hi,
A while ago, I posted this somewhere else.
I use it to create a menu structure, and I think in one site I converted it for a forum, but I cannot find it right now.
Anyhow, have a look. I think it is pretty understandable what happens. The idea is that you select all from a table, in which you store the message ID, a parentID (Which would be the message to which has been replied to) and a colum which defines a sortorder (In your coase you wouldn't need that, you can just sort by the unique ID> In a menu you may want to reshuffle pages). Then you start with the base-threads, where ID=0, and build from there, finding all the values which have a parent ecqual to the currently selected ID. It takes some tinkering, but this works for me, in several nested situations:
$menuquery = "select * from $menu_table order by M_sortorder";
$result=mysql_query($menuquery) or die('An error occured retrieving data');
$listing = Array();
while($row = mysql_fetch_array($result))
{
$listing[] = array("This_id" => $row[M_page_nr], "parent_id" => $row[M_p_nr],
"category_title" => $row[M_label], "link" => $row['M_link'], "path" => $row[M_path]);
}
function display_tree($parent, $level, $array, $menu_parent, $current_viewing_page)
{
foreach ($array AS $node)
{
if(($node[parent_id] == $parent) and ($node[This_id] <> $current_viewing_page))// If the parent matches the pageid in the array
{ // the current page is a child of this parent
if($node[This_id] == $menu_parent) // If the current page ID matches the parent_id, this child is a direct child
{
$selected = "selected";
}
else
{
$selected = "";
}
$indent = "";
for($i = 1; $i <= $level; $i++)
{
$indent .= "-";
}
$html .= "
<option $selected value=\"$node[This_id]\">$indent
$node[category_title]</option>
";
$html .= display_tree($node[This_id],
($level + 1), $array, $menu_parent, $current_viewing_page);
}
}
return $html;
} // end function display list
// Now use it
$pageslist = display_tree(0, 0, $listing, $menu_parent, $pageid);
$selected = "";