Tuesday, July 5, 2011

Listing subcategories or chilld categories using php

We can use the following function for getting the subcategories up to any level. It is using recursive technique.

function getSubCats($catid){
        $query= "select cat_id from category where cat_parent_id='$catid'";
        $rst =  $this->db->query($query);
        if($rst->num_rows==0){
            return null;
        }else{
            foreach($rst->rows as $row) {
                $categories[] = $row['cat_id'];
                $children = $this->getSubCats($row['cat_id']);   
                if($children!=null){
                    foreach($children as $child){
                        $categories[] = $child;   
                    }
                }
            }
           
            return $categories;
        }

    }

No comments:

Post a Comment