Dynamic Menu

<!-----This is for All menu----->
<?php 
$rootcatId= Mage::app()->getStore()->getRootCategoryId(); 
$categories = Mage::getModel('catalog/category')->getCategories($rootcatId);
function  get_categories($categories) {
    $array= '<ul>';
    foreach($categories as $category) {
        $cat = Mage::getModel('catalog/category')->load($category->getId());
        $count = $cat->getProductCount();
        $array .= '<li>'.
        '<a href="' . Mage::getUrl($cat->getUrlPath()). '">' . 
                  $category->getName() . "(".$count.")</a>\n";
        if($category->hasChildren()) {
            $children = Mage::getModel('catalog/category')->getCategories($category->getId());
             $array .=  get_categories($children);
            }
         $array .= '</li>';
    }
    return  $array . '</ul>';
}
echo  get_categories($categories); 
echo "<div class=\"clearer\"></div>";
?>
<!------End------->

<!------Only Current Menu > submenu-------->
<div class="left_menu">
	<?php
		$routeDetect = Mage::app()->getFrontController()->getRequest()->getRouteName();
		if($routeDetect == 'catalog') :
	?>
	<div class="block block-whishlist">
		<div class="block-title">
			<strong><span>Current Categories</span></strong>
		</div>
		<div class="block-content">
			<?php	
				$_current_category =Mage::registry('current_category')->getId();
				$collection = Mage::getModel('catalog/category')->getCategories($_current_category);
				$helper     = Mage::helper('catalog/category');	
			?>
			<ul>
				<?php foreach ($collection as $cat):?>
					<?php if($cat->getIsActive()):?>
						<li style="border-bottom: 1px solid #ddd">
							<a href="<?php echo $helper->getCategoryUrl($cat);?>">             
								<cite><?php echo $cat->getName();?></cite>
							</a>
							<?php
								$collection_sub = Mage::getModel('catalog/category')->getCategories($cat->getId());									
							?>
							<ul>
								<?php foreach ($collection_sub as $sub):?>
									<?php if($sub->getIsActive()):?>
										<li border-bottom: 1px solid #ddd>
											<a href="<?php echo $helper->getCategoryUrl($sub);?>">             
											&nbsp;<cite><?php echo $sub->getName();?></cite>
											</a>
										</li>	
									<?php endif;?>
								<?php endforeach;?>	
							</ul>		
						</li>
					<?php endif;?>
				<?php endforeach;?>
			</ul>	
		</div>
	</div>
	<?php endif;?>							
	<?php
		$routeDetect = Mage::app()->getFrontController()->getRequest()->getRouteName();
		if($routeDetect != 'customer') :
	?>
	<?php endif;?>
</div>
<!---------End--------->