I have category hierarchy like this.
- 721 parent is 235
- 235 parent is 201
- 201 parent is 1
- 1 parent is 0
0 is the root category id, I am trying to build a function that input leaf id 721, get full path id of 721, 235, 201, 1
public function getPath($inputId = 0, $idList=array())
{
$sql ="SELECT * FROM hierarchy where id='{$inputId}'";
$result = $this->db->fetchAll($sql);
if($result){
$currentId = $result[0]["id"];
$parentId = $result[0]["parent_id"];
$idList[] = $currentId;
if ($parentId !=0){
$this->getPath($parentId, $idList);
}else{
//var_dump($idList);
return $idList;
}
}
}
I can see correct results in var_dump part above, but when I use this function from another class, it return null, like this $data = $whateveHelper->getPath('721');
could anyone help?
Thanks
getPath.