PHP Tree Menu (Recursion Database)

message from David B on 23 Jul 2004
Have any of you ever made one of these PHP scripts that displays data in
a tree-like menu? I've been studying the tutorial at
http://www.sitepoint.com/article/hierarchical-data-database, and I can't
get it to work, though it looks really simple.

Finally, I gave up on my database table and made a new table, almost
exactly like the table in the tutorial. (I just renamed the field
"title" to "child.")

Below is the HTML for the entire page, including both scripts. I can't
display any data with the top script, the bottom script, or both
combined, even if I rename $node or $parent to $Food, $Fruit, etc.

Can you see anything wrong with my code?

<?php echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?".">";
[DATABASE CONNECTION]
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<html xmlns="http://www.w3.org/1999/xhtml"
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>

<?php
// $node is the name of the node we want the path of
function get_path($node) {
// look up the parent of this node
$result = mysql_query('SELECT parent FROM tree '.
'WHERE child="'.$node.'";');
$row = mysql_fetch_array($result);

// save the path in this array
$path = array();

// only continue if this $node isn't the root node
// (that's the node with no parent)
if ($row['parent']!='') {
// the last part of the path to $node, is the name
// of the parent of $node
$path[] = $row['parent'];

// we should add the path to the parent of this node
// to the path
$path = array_merge(get_path($row['parent']), $path);
}
// return the path
return $path;
echo $qry;
}
echo $qry;
?>

SECOND QUERY

<?php
// $parent is the parent of the children we want to see
// $level is increased when we go deeper into the tree,
// used to display a nice indented tree
function display_children($parent, $level) {
// retrieve all children of $parent
$result = mysql_query('SELECT child FROM tree '.
'WHERE parent="'.$parent.'";');

// display each child
while ($row = mysql_fetch_array($result)) {
// indent and display the title of this child
echo str_repeat(' ',$level).$row['child']."\n";

// call this function again to display this
// child's children
display_children($row['child'], $level+1);
}
echo $qry;
?>

</body>
</html>
 
Jason Dalgarno replied to David B on 23 Jul 2004
Yes.

If it makes you feel any better I don't understand that either, but I think
that says more about the quality of the article than either of us.

Not least echoing an apparently undefined $qry (one of which was after a
return) you don't call the functions.

<?php

function display_children($parent = '', $level = 0)
{
global $nodes;
if ($parent) {
echo str_repeat(' ', $level), $parent, "\n";
}
foreach ($nodes[$parent] as $child) {
display_children($child, $level + 1);
}
$nodes = array();
$result = mysql_query('SELECT parent, child FROM tree');
while ($row = mysql_fetch_assoc($result)) {
$nodes[$row['parent']][] = $row['child'];
}
display_children();

?>

Works in my head, I haven't tested it.
 
Michael Fesser replied to Jason Dalgarno on 23 Jul 2004
.oO(Jason Dalgarno)

Hmm, have tested it: I created the simple database and ran the first
script from the tutorial, copied & pasted 1:1. Works.

(Only thing I miss in the article is that the second approach is also
known as "nested sets")

Micha
 
David B replied to Jason Dalgarno on 23 Jul 2004
Finally! There's a bug that I'll work out, but your script works. I just
displayed the entire mammal kingdom (er, class). Thanks.
 
gareth replied to David B on 23 Jul 2004
Is that the code for the whole page?

You have 2 functions, but your not actually calling the functions from
anywhere so the code is never run.
 

Archived message: PHP Tree Menu (Recursion Database) (Macromedia Dreamweaver)