|
Posted by Nicholas Sherlock on 02/02/06 05:04
Hey all,
I wrote a class to represent a node in a site navigation tree which
contains methods for building the tree from the database. I also wrote
some routines to print out the whole tree, starting from the root.
This malfunctions in a weird way in PHP 4 (I get a truncated tree). In
desperation, I downloaded the trial of PHPEdit to use the debugger, and
the program worked flawlessly! I suspect that the difference is that
PHPEdit uses PHP 5 while my testing server and my production server are
using PHP 4 (Both were operating on the exact same data).
Can anyone spot any obvious problems?
//Display code, starting from node with id 7:
function display_tree($root, $indent)
{
for ($i=0; $i<$indent; $i++) {
echo " ";
}
echo $root->name."<br>\n";
for ($i=0; $i<$root->count(); $i++) {
display_tree($root->getchild($i),$indent+1);
}
}
$tree=new NavNode();
$tree->buildtree(7,null);
while ($tree->level()>0) {
$tree=$tree->parent;
}
display_tree($tree,0);
// The tree code:
//the highest node
$nav_rootnode=null;
//represents a node in a navigation tree
class NavNode {
var $children;
var $parent, $parentid, $id;
var $pageid;
var $name;
function count()
{
return count($this->children);
}
function getchild($index)
{
return $this->children[$index];
}
function level()
{
if (isset($this->parent)) {
return $this->parent->level()+1;
} else {
return 0;
}
}
function NavNode()
{
$this->children=array();
}
function buildup($id, &$childnode)
{
//find out who our parents are
$result=mysql_query("SELECT parent,pageid,name,flags FROM
structure WHERE id=".$id) or die("Buildup: ".mysql_error());
if (mysql_num_rows($result)==0) {
exit;
//we don't actually exist
}
$row=mysql_fetch_row($result);
$this->id=$id;
$this->parentid=$row[0];
$this->pageid=$row[1];
$this->name=$row[2];
//build our parent
if ($this->parentid != 0) {
$this->parent=new NavNode();
global $nav_rootnode;
$nav_rootnode=$this->parent;
$this->parent->buildup($this->parentid,$this);
}
$this->builddown($childnode);
}
function builddown(&$childnode)
{
//build our children
$result=mysql_query("SELECT id,pageid,name,flags FROM structure
WHERE parent=".$this->id) or die("Build down: ".mysql_error());
while ($row=mysql_fetch_row($result)) {
//we've already built and been provided with this child
if (isset($childnode) && ($childnode->id==$row[0])) {
array_push($this->children,$childnode);
continue;
}
$child=new NavNode();
$child->id=$row[0];
$child->pageid=$row[1];
$child->name=$row[2];
if ($row[3]=="expandall") {
$child->builddown(null);
}
array_push($this->children,$child);
}
}
function buildtree($id,$childnode)
{
$this->buildup($id,$childnode);
}
}
function buildlinkbox($node, $level)
{
for ($i=0; $i<$node->count(); $i++) {
for ($s=0; $s<$level; $s++) {
echo " ";
}
$child=$node->getchild($i);
echo "<a
href='".$_SERVER['PHP_SELF']."?id=".$child->id."'>".$child->name."</a><br>\n";
buildlinkbox($child,$level+1);
}
}
Thanks so much if anyone can help me out here.
Cheers,
Nicholas Sherlock
Navigation:
[Reply to this message]
|