1

I'm using this query:

select cat.*, cv.value as `category_name` from `catalog_category_entity` as cat
    join `catalog_category_entity_varchar` as cv on cat.entity_id = cv.`entity_id`
    join `eav_attribute` as att on att.`attribute_id` = cv.`attribute_id`
    join `eav_entity_type` as aty on att.`entity_type_id` = aty.`entity_type_id`
    where aty.`entity_model` = 'catalog/category' and att.`attribute_code` = 'name' and cv.`store_id` = 0  and cv.`entity_id` != 2 and cv.`entity_id` != 1

but data is not coming according to category hierarchy parent to child, data is coming random.

1
$categories = Mage::getModel('catalog/category')
            ->getCollection()
            ->addAttributeToSelect('*')
            ->addIsActiveFilter()
            ->addAttributeToFilter('include_in_menu','1');

it gives category list with category id category name as well as level of category

1
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
        <ul class="level1">
            <?php foreach($_categories as $_category): ?>
                <li class="parent">
                    <a id="<?php echo $_category->getId() ?>" href="javascript:void(0)">
                        <?php echo $_category->getName() ?>
                    </a>
                    <?php //if ($currentCategory && $currentCategory->getId() == $_category->getId()): ?>
                        <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                        <?php $_subcategories = $_category->getChildrenCategories() ?>
                        <?php if (count($_subcategories) > 0): ?>
                            <ul class="level2">
                                <?php foreach($_subcategories as $_subcategory): ?>
                                    <li class="parent parent2">
                                        <a  id="<?php echo $_subcategory->getId() ?>" href="javascript:void(0)">
                                            <?php echo $_subcategory->getName() ?>
                                        </a>

                         <?php $_category1 = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>
                         <?php $_subcategories1 = $_category1->getChildrenCategories() ?>
                         <?php if (count($_subcategories1) > 0): ?>
                             <ul class="level3">
                                 <?php foreach($_subcategories1 as $_subcategory1): ?>
                                 <?php $currentCategoryId===$_subcategory1->getId() ?>
                                     <li class="parent parent3">
                                         <a id="<?php echo $_subcategory1->getId() ?>" href="javascript:void(0)">
                                             <?php echo $_subcategory1->getName() ?>
                                         </a>

                                     <?php $_category2 = Mage::getModel('catalog/category')->load($_subcategory1->getId()) ?>
                                         <?php $_subcategories2 = $_category2->getChildrenCategories() ?>
                                         <?php if (count($_subcategories2) > 0): ?>
                                            <ul class="level4">
                                                 <?php foreach($_subcategories2 as $_subcategory2): ?>
                                                     <li class="parent parent4">
                                                         <a id="<?php echo $_subcategory2->getId()  ?>" href="javascript:void(0)">
                                                             <?php echo $_subcategory2->getName() ?>
                                                         </a>
                                                     </li>
                                                 <?php endforeach; ?>
                                             </ul>
                                         <?php endif; ?>

                                     </li>
                                 <?php endforeach; ?>
                             </ul>
                         <?php endif; ?>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        <?php endif; ?>
                    <?php // endif; ?>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php endif;?>
0

This is a copy from the previous answer but without <ul> & <li> Tags:

<?php 
    $_helper = Mage::helper('catalog/category');
    $_categories = $_helper->getStoreCategories();
    $currentCategory = Mage::registry('current_category');
    $listCats = array();
    if (count($_categories) > 0) {
        foreach($_categories as $_category) {
            $listCatsTemp = array();
            $listCatsTemp['id'] = $_category->getId();
            $listCatsTemp['name'] = $_category->getName();
            $listCatsTemp['child'] = array();
            $_category = Mage::getModel('catalog/category')->load($_category->getId()); 
            $_subcategories = $_category->getChildrenCategories();
            if (count($_subcategories) > 0) {
                $listSubCats = array();
                foreach($_subcategories as $_subcategory) {
                    $listSubCatsTemp = array();
                    $listSubCatsTemp['id'] = $_subcategory->getId();
                    $listSubCatsTemp['name'] = $_subcategory->getName();
                    $listSubCatsTemp['child'] = array();
                    $_category1 = Mage::getModel('catalog/category')->load($_subcategory->getId());
                    $_subcategories1 = $_category1->getChildrenCategories();
                    if (count($_subcategories1) > 0) {
                        $listSubSubCat1 = array();
                        foreach($_subcategories1 as $_subcategory1) {
                            $listSubSubCatTemp = array();
                            $listSubSubCatTemp['id'] = $_subcategory1->getId();
                            $listSubSubCatTemp['name'] = $_subcategory1->getName();  
                            $listSubSubCatTemp['child'] = array();
                            $_category2 = Mage::getModel('catalog/category')->load($_subcategory1->getId());
                            $_subcategories2 = $_category2->getChildrenCategories();
                            if (count($_subcategories2) > 0) {
                                $listSubSubCat2 = array();
                                foreach($_subcategories2 as $_subcategory2) {
                                    $listSubSubCatTemp1 = array();
                                    $listSubSubCatTemp1['id'] = $_subcategory2->getId();
                                    $listSubSubCatTemp1['name'] = $_subcategory2->getName();
                                    $listSubSubCat2[] = $listSubSubCatTemp1;

                                } // end foreach sub cat 2

                            } else {
                                $listSubSubCat2 = array();

                            }  // end if sub cat 2
                            $listSubSubCatTemp['child'] = $listSubSubCat2;
                            $listSubSubCat1[] = $listSubSubCatTemp;

                        } // end foreach sub cat 1

                    } else {
                        $listSubSubCat1 = array();

                    }  // end if sub cat 1
                    $listSubCatsTemp['child'] = $listSubSubCat1;
                    $listSubCats[] = $listSubCatsTemp;

                } // end foreach sub cat

            } else {
                $listSubCats = array();

            }  // end if sub cat
            $listCatsTemp['child'] = $listSubCats;
            $listCats[] = $listCatsTemp;

        }  // end foreach cats

    }  // end if cats


    // printing result
    error_reporting(E_ALL); ini_set('display_errors', 1);
    echo '<pre>'; print_r($listCats); echo '</pre>';
?>

The result will be like this: i have many cats look like this

Array
(
    [0] => Array
        (
            [id] => 139
            [name] => WOMEN
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 154
                            [name] => Dresses
                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 223
                                            [name] => Party Dresses
                                            [child] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [id] => 183
                            [name] => Jumpsuit
                            [child] => Array
                                (
                                )

                        )

                    [2] => Array
                        (
                            [id] => 157
                            [name] => Tops
                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 165
                                            [name] => T-Shirt
                                            [child] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [3] => Array
                        (
                            [id] => 158
                            [name] => Bottoms
                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 173
                                            [name] => Trousers
                                            [child] => Array
                                                (
                                                )

                                        )

                                )

                        )

                    [4] => Array
                        (
                            [id] => 174
                            [name] => Short
                            [child] => Array
                                (
                                )

                        )

                    [5] => Array
                        (
                            [id] => 176
                            [name] => Jean Shorts
                            [child] => Array
                                (
                                )

                        )

                    [6] => Array
                        (
                            [id] => 155
                            [name] => Skirts
                            [child] => Array
                                (
                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 138
            [name] => MEN
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 146
                            [name] => Best Seller
                            [child] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 150
                            [name] => Destroyed Jeans
                            [child] => Array
                                (
                                )

                        )

                )

        )

    [2] => Array
        (
            [id] => 140
            [name] => SALE
            [child] => Array
                (
                    [0] => Array
                        (
                            [id] => 163
                            [name] => Men
                            [child] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [id] => 164
                            [name] => Women
                            [child] => Array
                                (
                                )

                        )

                )

        )

)
0
    private function buildCategoryArray(array $elements, $parentId = 0) {
        $branch = array();
        foreach ($elements as $element) {
            if ($element['parent_id'] == $parentId) {
                $subCat = $this->buildCategoryArray($elements, $element['id']);
                if ($subCat) {
                    $element['subCategory'] = $subCat;
                }
                $branch[] = $element;
            }
        }
        return $branch;
    }

And call function like this $rows1 = $this->buildCategoryArray($rows);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.