Monday, February 20, 2012

Function for getting all parents of a child category in php

Function for getting all parents of a child category in php

function getRootParent($id,$cats=array())
    {
        $this->db->select('cat_id,cat_parent_id');
        $this->db->where('cat_id', $id);
        $result = $this->db->get('category');
            foreach($result->result_array() as $ids){
                if($ids['cat_parent_id']==0){
                $cats[] = $ids['cat_id'];
                    return $cats;
                }else{
                $cats[] = $ids['cat_id'];
                    $cats = $this->getRootParent($ids['cat_parent_id'],$cats);
                }
            }
      
        return $cats;
    }

No comments:

Post a Comment