I'm Newbie - sorry if this is the wrong list!
I'm trying to print this treestructure I've made, but it doesn't seem to print more
than the first level. The
subtree array of the children is empty, perhaps because I do not
use the correct reference passing?
Thank you for helping me out.
This is the code:
<?php require('database.php'); // $databaseis defined here $database = new db(); class
TreeNode
{
var $data; //Database object reference
var $id; //Unique ID from the database
var $subtree; //Array of treenodes that are the children of $currentNode
var $level; //Level in the tree. The toplevel = 0 function
TreeNode($id, $data, $level) //Constructor
{
$this->id = $id;
$this->data = $data;
$this->level = $level;
$this->subtree = array();
} function add($currentNode)
{
//This function is used to add a TreeNode, $currentNode to the subtree
print "parent: ". $this->id ." New node: ". $currentNode->id ."<br>";
$this->subtree[] = $currentNode;
} function output()
{
for ($i=0;$i<count($this->subtree);$i++)
{
$tree =& $this->subtree[$i];
if (is_object($tree))
print "ID: ".$tree->id."<BR>";
print "Count: ".count($tree->subtree)."<BR>";
$tree->output();
} /* foreach ($this->subtree as $tree) //Skal være en while
løkke, da $tree->subtree er tomt.
{
if (is_object($tree))
print "ID: ".$tree->id."<BR>";
$tree->output();
}
*/
}
} //Initialize
$treetop = new TreeNode(0, 0, -1); // You have to have a start position
$nodehash = array(); // This is only used to find the
parents //Make the query
$sql = "SELECT * FROM page_table WHERE visible=1 ORDER BY id, title";
$query = new query($database, $sql); while ($obj = $query->getobj())
{
if($obj->parent_id > 0){ //If the node is not on the toplevel
$parent =& $nodehash["id_$obj->parent_id"]; //Finds the
parent in the $nodehash
}
else
{
$parent =& $treetop; //If the node is on
the toplevel then the parent is $treetop
}
$level = $parent->level + 1; //The tree level of $currentNode is of
course one more than the $parent (level
is not stored in db) //when we have found the level of $currentnode we are ready to
construct the $currentNode using the parameters
from the database and the $level
$currentNode = new TreeNode($obj->id, $obj, $level);
$nodehash["id_$obj->id"] = $currentNode; //Insert the $currentNode
into the $nodehash so we can find it again
if it has any children
$parent->add($currentNode); //Use the add function of the $parent to
put $currentNode into $parent's subtree
} //Something has to be printed out...
$treetop->output();
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]