A while back ago I created a simple tree walking using recursive.
and I got alot of feedback and solutions, but now when I mingle it with other code I have lost controll, can anybody help me see through the code below and see what is wrong, please!
best regards Thomas
Code:
It returns the results but I get alot of error messages at first:
x*
Warning: Invalid argument supplied for foreach() in C:\www\html\SLAdmin\www\admin@menu\rec_test6.php on line 20
<?include_once("./mysql.php")?>
<?
$tree = buildTree(0,0,'-');
foreach($tree as $leaf) {echo $leaf . "<br>\n";}
function buildTree($id = 0, $cnt = 0, $idnt = '*', $running = 0) {
global $db;
//if we are not in a recursive loop
if(!$running || !is_numeric($running)) {
if(!is_numeric($id)) {return -1;}
if(!is_numeric($cnt)) {return -1;}
if(!is_numeric($cnt)) {return -1;}
} //end if
$retVal = array();
$names = $db->get_results("SELECT * FROM names WHERE parent = ". $id ."");
foreach ($names as $name) {
array_push($retVal,(str_repeat($idnt,$cnt) . $name->name."$running"));
$retVal = array_merge($retVal,buildTree($name->id, $cnt + 1, $idnt, 1));
}
return($retVal);
} //end build tree
?>
Thread:
http://www.phpbuilder.com/board/showthread.php?threadid=10264640
<?include_once("./mysql.php")?>