Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Problems loading/displaying categories
 
eightonegulf
Jr. Member
 
Total Posts:  1
Joined:  2012-01-20
 

Hello,

We’ve been trying to program a menu, which displays all main categories and all relevant nested categories. This works at the moment, but the problem is it takes way too long to load the categories. This can take between 6 to 15 seconds. We’ve found the issue lays with the following lines.

$_helper Mage::helper('catalog/category');
    
$_categories $_helper->getStoreCategories();

This retrieves only the main categories. Funny thing is, fetching the subcategories of a categories is a matter of milliseconds. My guess is when fetching the main categories, ALL categories are loaded. We have quite a lot of categories so that’s probably causing the problems.

Is there another way to fetch the main categories, which doesn’t take as long?

For the interested, here is the entire piece of code.

<?php
    
function display_category($_category$curCategoryPath$depth){
        
echo '<ul style="padding-left: '. ($depth*7) . 'px;">';
    

        
$cdata Mage::getModel('catalog/category')->load($_category->getId());
        if(
in_array($_category->getId(), $curCategoryPath)){
            
//Display children
            
            
echo '<li>
                    <table>
                        <tr>
                            <td>
                                <font><a style="text-decoration: none;" href="'
.$cdata->getUrl().'">-- </a></font>
                            </td>
                            <td>
                                <a href="'
$cdata->getUrl(). '" style="text-decoration: underline;">'$_category->getName(). '</a>
                            </td>
                        </tr>
                    </table>
                </li>'
;
            
            
            foreach (
$_category->getChildren() as $_subcategory){
                display_category
($_subcategory,$curCategoryPath,$depth+1);
            
}
        }else{
            
echo '<li>
                    <table>
                        <tr>
                            <td>
                                <font><a style="text-decoration: none;" href="'
.$cdata->getUrl().'">+ </a></font>
                            </td>
                            <td>
                                <a href="'
$cdata->getUrl(). '" style="text-decoration: underline;">'$_category->getName(). '</a>
                            </td>
                        </tr>
                    </table>
                </li>'
;
        
}
        
echo "</ul>";
    
}

    
    $startTime 
microtime(true);
    
    
$_helper Mage::helper('catalog/category');
    
$_categories $_helper->getStoreCategories();    
    
//echo " " . (microtime(true)-$startTime);
    
    
$curCategoryPath = array();

    if(
Mage::registry('current_category')){
        $temp 
Mage::registry('current_category');
        while(
$temp!=NULL && $temp->getName()!=NULL && $curDepth<$maxDepth){
            $curCategoryPath[] 
$temp->getId();
            
$temp $temp->getParentCategory();
            
$curDepth++;
        
}
    }

    
foreach ($_categories as $_category){
        display_category
($_category$curCategoryPath0);
    
}
    

?>
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top