i cretated a class for a creation of a tree view starting from a database like [iditem, parentitem, description)
this works correctly under PHP 5 but not on PHP4
the problem stands in the recursive call and assignment to subchilds it seems that php4 loses the references to the children objects in some bad way...
anoyone got the same problem and knows how to get around it?
<?php
class TreeNode
{
var $_description;
var $_internalTable;
function TreeNode($description)
{
$this->_description = $description;
$this->_internalTable = array();
}
function description($valore = null)
{
if ($valore != null)
$this->_description = $valore;
else
return $this->_description;
}
function addNode($node)
{
$this->_internalTable[] = $node;
}
function hasChilds()
{
echo count($this->_internalTable);
return (count($this->_internalTable) != 0);
}
function printTree($lev = 0)
{
$rientro = "";
for ($i = 0; $i < $lev; $i++)
$rientro .= " ";
print_r($this->_internalTable);
if ($this->hasChilds() == true)
{
echo $rientro . "+" . $this->description() . "<br>\n";
foreach ($this->_internalTable as $node)
echo $node->printTree($lev + 1);
}
else
echo $rientro . "-" . $this->description() . "<br>\n";
}
}
function buildTree(&$tree, $dbArray, $parentId)
{
foreach ($dbArray as $id => $element)
{
if ($element['parent'] == $parentId)
{
$newTree = new TreeNode($element['description']);
$tree->addNode($newTree);
buildTree($newTree, $dbArray, $id);
}
}
}
// ------------------------------------------------------------------------------------------------------ //
$conn = mysql_connect($ip, $uname, $pwd);
mysql_select_db($dbName, $conn);
$dbres = mysql_query("SELECT * FROM $table", $conn);
//$table structure (id, parent, description) ->parent points to id of a parent node in the same table
mysql_close($conn);
$nodes = array();
while ($row = mysql_fetch_array($dbres))
{
$attrs = array();
$attrs['parent'] = $row['parent'];
$attrs['description'] = $row['description'];
$nodes[$row['id']] = $attrs;
}
echo "<pre>\n\n";
//apd_set_pprof_trace();
$treeView = new TreeNode("ROOT");
buildTree($treeView, $nodes, 0);
$treeView->printTree();
echo "\n\n</pre>";
?>