PHP Recursive database loop -


i want main parent of chosen subcategory. made function recursively loop databasse , id can echo it. cannot return variable. need work further returned id. here code.

public function check_parent($parent) {     $q = $this->db->get_where('ci_categories', array('cat_id'=>$parent));     $r = $q->row();     if ($r->cat_child > 0)     {         $this->check_parent($r->cat_child);     }   else {         echo $parent;     } } 

when use return $parent in else null. ideas ?

if want return value should return on both places

public function check_parent($parent) { $q = $this->db->get_where('ci_categories', array('cat_id'=>$parent)); $r = $q->row(); if ($r->cat_child > 0) {     return $this->check_parent($r->cat_child); }   else {     return $parent; } 

Comments