Re: Treestructure
| From: | Johan Evers Petersen | Date: | Fri, 20 Apr 2001 07:44:50 +0000 |
| Subject: | Re: Treestructure | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-49529@lists.php.net to get a copy of this message | ||
Yes thx, but it seems that my tree is build allright, I just can't get it
out on print...
I have in my add() function a printout and from this I believe that the tree
is build allright. All the treenodes are added to the correct subtree
arrays.
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;
}
""Chris Lee"" <lee@mediawaveonline.com> wrote in message
news:9bo0a6$v92$1@toye.p.sourceforge.net...
this is my make_tree() function like your make_node() I think
function _make_tree($category_id)
{
global $database;
static $padding = -1;
foreach($database->select_array('', 'category', "WHERE
category_parent =
$category_id ORDER BY category_name") as $pos => $result)
{
$id = $result['category_id'];
$category_id[$id] = $result['category_id'];
$category_parent[$id] = $result['category_parent'];
$category_name[$id] = $result['category_name'];
}
$padding++;
if (isset($category_id))
foreach ($category_id as $pos => $val)
{
$this->category_id[$pos] = $category_id[$pos];
$this->category_parent[$pos] = $category_parent[$pos];
$this->category_name[$pos] = $category_name[$pos];
$this->level[$pos] = str_repeat(' ', $padding * 2);
$this->_make_tree($category_id[$pos]);
}
$padding--;
}
all you need is three fields, the categories id, the categories parent, and
the categories name. recusion is tricky stuff :)
--
Chris Lee
lee@mediawaveonline.com
""Johan Evers Petersen"" <johan@vinorama.dk> wrote in message
news:9bnn77$524$1@toye.p.sourceforge.net...
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: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net