I have this recursive function,and I want only to display the branches up to 10 levels only,but my filtering is not working.
public function display_children($parent,$level, $rec = array()){
try {
$cmd = $this->connection->prepare('SELECT mem,pid from mytree where pid = ?');
$cmd->execute(array($parent));
while ( $row = $cmd->fetch(PDO::FETCH_ASSOC)) {
$rec[] = [['v' => $row['mem'], 'f' => $row['mem']], (string)$row['pid'], $row['mem']];
if($level >10){
return $rec;
exit();
}
$rec = $this->display_children($row['mem'], $level + 1, $rec);
}
}
catch(PDOException $ex){
return $ex->getMessage();
}
return $rec;
}
Thank you in advance.