2

enter image description here

I have Used recursive function to get the category full path like Access Control/CARDS/FOBS from above table structure but my recursive function return null value.

function xyz($id,$parent) {

if($parent == '0') {
   //my code working fine
   //return 
} 
else 
{
    $catid = $id; //here 25 coming

    $cat_array        =  array();
    $category_array   =  $this->Recursive($catid,$cat_array);   
    //echo $category_array;exit; getting Null result
    return $category_array ;

    }

}

function Recursive($catid,$cat_array) {

  $sql       = mysql_query("Select bg_category_id,parent_id,title from categories_list 
               Where bg_category_id = '".$catid."'");
  $result    = mysql_fetch_array($sql);

  array_push($cat_array,$result['title']); 

  if($result['parent_id'] != '0') {

     $this->Recursive($result['parent_id'],$cat_array) ;

  } else {  

    if(count($cat_array) > 0){
       $k =  implode("/",$cat_array);
     }
     //echo $k;exit; getting desired result FOBS/CARDS/Access Control 
    return $k;

  }

}

Active user
  • 145
  • 1
  • 13

1 Answers1

4

You need to return the recursive function when you recurse, otherwise it will return nothing.

if($result['parent_id'] != '0') {

   return $this->Recursive($result['parent_id'],$category_array) ;

 } 
dave
  • 57,127
  • 4
  • 69
  • 87