I am trying to find out the parents of a new member till root. For this, I have written a recursive function in PHP that is expected to work, but results are not as expected.
$parlist=array();
function parlistf($child, $con, &$parlist)
{
$qry="SELECT par from users where sno='$child'";
$res=$con->query($qry);
$row=$res->fetch_object();
if($row->par>0)
{
echo "<br>-----<br>$row->par<br>-----<br>";
$parlist[] = parlistf($row->par, $con, $parlist);;
var_dump($parlist);
}
}
parlistf($newid, $con, $parlist);
echo "<br>";
var_dump($parlist);
Result of above code is as under:
-----
15
-----
-----
7
-----
-----
3
-----
-----
1
-----
array(1) { [0]=> NULL } array(2) { [0]=> NULL [1]=> NULL } array(3) { [0]=> NULL [1]=> NULL [2]=> NULL } array(4) { [0]=> NULL [1]=> NULL [2]=> NULL [3]=> NULL } array(4) { [0]=> NULL [1]=> NULL [2]=> NULL [3]=> NULL }
As your can see here, echo is working fine but values are not stored in the array. Where I am doing wrong?
$parlist[] = parlistf($row->par, $con, $parlist);;has two;and I dont see what return it is feeding the array.&$parlist