I have written a function with while loop for getting recursive parents of category from DB in PHP, it fulfilled my purpose. But I want to know (actually curious to know) that how can I do this by recursive function? If yes then how? All it is doing is that it returns an array of parent categories by using a categoroy_id. Please let me know if something is unclear.
public function get_recursive_parents($category_id){
$categories = array();
$res = $this->db->from('categories')->where('cat_id',$category_id)->get()->row_array();
$cat_id = $res['parent_id'];
$categories[] = $res;
while($cat_id){
$res = $this->db->from('categories')->where('cat_id',$cat_id)->get()->row_array();
$categories[] = $res;
$cat_id = $res['parent_id'];
}
return $categories;
}
$res['parent_id']
is false, 0 or empty. – Dainis Abols Feb 13 '13 at 12:37