I'm struggling with a recursive function as it seems to be overwriting the variables in the foreach loop. Can someone help me identify how to fix this? What I'm trying to accomplish is a folder tree, found in a SQL database, where the parent folder holds children and grandchildren. I'm getting children, but only grandchildren of the first child. I believe it's overwriting the statement somewhere.
function locateFolders($folderId, $arrIn = array()) {
$con = $_SESSION["dbConnection"]->conStr;
array_push($arrIn, $folderId);
// Select all folders that have this id as the parent folder id
$statement = "SELECT * FROM folders WHERE parentid='$folderId'";
$result = mysqli_query($con, $statement) or die(mysqli_error($con));
if (mysqli_num_rows($result) > 0) {
while ($r = mysqli_fetch_assoc($result)) {
array_push($arrIn, $r["id"]);
$statement2 = "SELECT * FROM folders WHERE parentid='".$r["id"]."'";
$result2 = mysqli_query($con, $statement) or die(mysqli_error($con));
while ($row = mysqli_fetch_assoc($result2)) {
return locateFolders($row["id"], $arrIn);
}
}
}
$arrIn = array_unique($arrIn);
return $arrIn;
}