Get all child category under a category

## get all childs & their childs under a parent category


//get all childs & their childs

public function get_child_categories( $parent_id ){

$childtree = $this->nowbuild_child_tree($parent_id);

echo "<pre>";
print_r($childtree);
echo "</pre>";

}

//now build all child tree against parent id

public function nowbuild_child_tree( $parent_id ) {

$sqlquery = " SELECT * FROM categories where parent='" .$parent_id . "' ";

$row = $this->db->query($sqlquery)->result_array();

$allCategory = array();

if(count($row)>0){

foreach ($row as $value) {

 $tempCat = array();
 $cat_id  = $value['id'];     $tempCat['categoty_details'] = $value;

//has sub category
$hasSubCat = $this->nowbuild_child_tree($cat_id);
               if(count($hasSubCat) >0 ){

$tempCat['has_sub'] = 'Y';  $tempCat['sub_category'] = $this->nowbuild_child_tree($cat_id);
            //unset($tempCat['sub_category']);

 }else{                $tempCat['has_sub']     = 'N';

 }
                array_push($allCategory, $tempCat);
            }
    }
    return $allCategory;  
    }
Please follow and like us:
0