I am trying to calculate parent names of a person recursively to a specific person ID. I've written recursive call for this but it is behaving weirdly. I've tried to debug it but still don't get what I am doing wrong with it.
Here is my script:
public function full_name_to_root()
{
$name = $this->name;
// printf('caller');
$full_name = FamilyTree::getNameToRoot($this,$name,1);
dd($full_name);
return $full_name;
}
public static function getNameToRoot($daleel,$name,$c)
{
$halt = false;
$c+=1;
echo 'in rec. <br>';
$parent = $daleel->parent;
if(!$parent){
$halt = true;
return $c;
}elseif ($parent && $parent->family_tree_id==6) {
echo 'Reached to where we want to stop <br>';
$name .= ' '.$parent->name;
$halt = true;
return $c; // As you can see in output when control reaches it is not stopping the execution, but it is always going to end
}elseif($parent){
echo 'call again <br>';
$name .= ' '.$parent->name;
$halt = false;
FamilyTree::getNameToRoot($parent,$name,$c);
}
// Control should never reach here but it is coming here every time and more annoying thing is that it is printing highest count(or in case of name the most large, which also the required one) at first time and then gradually prints lower
echo "<br>";
echo $c;
echo "<br>";
// function executes above echo statements 6-7 times but only executes the return statement last time.
return $c; // I am sending count in response for just testing purpose. It is behaving same as $name is behaving.
}
I have adding some flags for debugging purpose here is the output of this function.
Any help sorting out this issue would be amazing. Thanks!