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;

  }

}

Monojit
  • 157
  • 1
  • 15

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
  • 62,300
  • 5
  • 72
  • 93
  • I have modified my post. Sorry for previous post. But till now I am getting null value. – Monojit Feb 03 '14 at 06:09
  • @user3051318 You still don't have a `return` statement on this line. – Barmar Feb 03 '14 at 08:06
  • @dave thanks a lot.Your code works fine. Can you please explain why it needs return because my Recursive function already return one value. – Monojit Feb 03 '14 at 09:21