Tuesday, July 5, 2011

Finding root parent of a child category in php

Using the below recursive function we can able to find the root parent category of a child category.

function getRootParent($id){
             $query = "select nav_parent_id from navigation_categories where nav_id='$id'";
            $result = $this->db->query($query);
                foreach($result->rows as $ids){
                    if($ids['nav_parent_id']==0){
                        return $id;
                    }else{
                        return $this->getRootParent($ids['nav_parent_id']);
                    }
                }
           
            return $id;
        }

No comments:

Post a Comment