Display multilevel subcategory of a category using single table


Today i am going to show all category and its sub-category by using a single table like below example -


My example table like below. In below table all the title which cat_id is '0' that are main category and rest of others are sub category to there parent id.




Now my requirement is to get all category with its subcategory in array .To fulfill this requirement i make a function getCategoryForParentId (parentId=0) like below -

x------------------x----------------x----------------------------------------------------------------------x

function getCategoryForParentId($parent_id = 0) {
$categories = array();
$sql = "SELECT id, cat_id, title FROM category WHERE cat_id='".$parent_id."' ORDER BY title asc";
$result = $this -> result_array($sql);

foreach ($result as $mainCategory) {
$category = array();
$category['id'] = $mainCategory['id'];
$category['name'] = $mainCategory['title'];
$category['parent_id'] = $mainCategory['cat_id'];
$category['sub_categories'] = $this->getCategoryForParentId($category['id']);
$categories[$mainCategory['id']] = $category;
}
return $categories;
}

x--------------------x---------------------------x--------------------------x------------------------------x


Above function return an array with related category and it's subcategory like below example array-


Array
(
    [43] => Array
        (
            [id] => 43
            [name] => ASTM
            [parent_id] => 3
            [sub_categories] => Array
                (
                )

        )

    [44] => Array
        (
            [id] => 44
            [name] => DIN
            [parent_id] => 3
            [sub_categories] => Array
                (
                    [50] => Array
                        (
                            [id] => 50
                            [name] => BS
                            [parent_id] => 44
                            [sub_categories] => Array
                                (
                                    [52] => Array
                                        (
                                            [id] => 52
                                            [name] => UNE
                                            [parent_id] => 50
                                            [sub_categories] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [49] => Array
                        (
                            [id] => 49
                            [name] => ISO
                            [parent_id] => 44
                            [sub_categories] => Array
                                (
                                    [51] => Array
                                        (
                                            [id] => 51
                                            [name] => GOST
                                            [parent_id] => 49
                                            [sub_categories] => Array
                                                (
                                                )

                                        )

                                )

                        )

                )

        )

)
 
 
Below is my html :

<div class="left_colom">
               <span class="categories_box"> Categories</span>
            <ul class="left_nav">
                <?php
                $categoryAll=$category->getCategory();
                if(!empty($categoryAll)){
                    foreach($categoryAll as $cat){
                        $subcat=$category->getCategoryForParentId($cat['id']);
                        echo "<pre>";
                        print_r($subcat);die;
                                           
                ?>
                <li <?=(!empty($subcat)) ? "id='category'" :""?>><a href="<?=(empty($subcat)) ? $BASE_PATH."product_list.php?catid=".$cat['id'] : "javascript:void()" ?>" title=""><?=$cat['title']?> </a>
                    <?php if(!empty($subcat)){?>
                    <ul style="display:none;" id="subcat">
                    <?php foreach($subcat as $subcatogry){?>
                    <li><a href="#" title=""><?=$subcatogry['name']?></a>
                        <?php if(!empty($subcatogry['sub_categories'])){?>
                        <ul>
                            <?php foreach($subcatogry['sub_categories'] as $subSubcat){?>
                            <li><a href="#" title="ISO"><?=$subSubcat['name']?></a>
                                <?php if(!empty($subSubcat['sub_categories'])){?>
                                <ul>
                                    <?php foreach($subSubcat['sub_categories'] as $subSubSubcat){?>
                                    <li><a href="#" title="GOST"><?=$subSubSubcat['name']?></a></li>
                                    <?php }?>
                                </ul>
                                <?php }?>
                            </li>
                            <?php }?>
                        </ul>
                        <?php }?>
                    </li>
                    <?php }?>
                    </ul>
                </li>
                <?php }} }else{
                    echo '<li><a href="javascript:void();" style="color:red;">Sorry no category found.</a></li>';
                  }
                ?>

            </ul>
</div>


Don't forgot to leave a comment if you like this

Happy coding :)

3 Comments

  1. How could i use this in zend view like this->allcat

    ReplyDelete
  2. hi
    Multi level category multiselect CodeIgniter
    https://github.com/hamedhossani/Multi_level_category_multiselect_CodeIgniter

    ReplyDelete
Previous Post Next Post