Get all category and subcategory by recursion function.

Get all category and subcategory by recursion function.
Use recursion calling function get all category and subcategory.
Create function 'getCate' pass the parameter '$categories'. Using ol and li tag print the category.
Firstly get root category ID and load ...


Here is full code.....
<?php
$rootcateIds= Mage::app()->getStore()->getRootCategoryId();
$categories = Mage::getModel('catalog/category')->getCategories($rootcateIds);

function  getCate($categories)
{
$arr= '<ol>';
foreach($categories as $category)
{
$cat = Mage::getModel('catalog/category')->load($category->getId());
$count = $cat->getProductCount();
$arr .= '<li>'.
'<a href="' . Mage::getUrl($cat->getUrlPath()). '">' . $category->getName() . "(".$count.")</a>\n";

if($category->hasChildren())
{
$child = Mage::getModel('catalog/category')->getCategories($category->getId());
$arr .=  getCate($child);
}
$arr .= '</li>';
}
return  $arr . '</ol>';
}
echo  getCate($categories);
?>

Labels: