1

Right now my code is

<?php
$cats = Mage::getModel('catalog/category')->load(2)->getChildren();
$catIds = explode(',',$cats);
$i=0;
?>
<ul class="decor1_1">
<?php foreach($catIds as $catId): ?>
    <li class="active">
        <?php
            $category = Mage::getModel('catalog/category')->getCollection();
            $category->addAttributeToFilter("Top_Menu" , array('eq' => '146'));
            $category->addAttributeToFilter("entity_id" , array('eq' => $catId));
            $category->addAttributeToSelect('*');
            $category->load();
            foreach($category as $category1): 
                echo '<a href="'.$category1->getUrl().'">'.$category1->getName()."</a>";
                    $subCats = Mage::getModel('catalog/category')->load($category1->getId())->getChildren();
                    $subCatIds = explode(',',$subCats);
                    if(count($subCatIds) > 1):?>
                    <div class="white-bg" style="height:634px;">
                        <ul>
                        <?php foreach($subCatIds as $subCat) :?>
                            <li>
                            <?php
                                $category2 = Mage::getModel('catalog/category')->getCollection();
                                $category2->addAttributeToFilter("Top_Menu" , array('eq' => '146'));
                                $category2->addAttributeToFilter("entity_id" , array('eq' => $subCat));
                                $category2->addAttributeToSelect('*');
                                $category2->load();
                                foreach($category2 as $category3): 
                                    echo '<a href="'.$category3->getUrl().'">'.$category3->getName()."</a>";
                                    $subCats2 = Mage::getModel('catalog/category')->load($category3->getId())->getChildren();
                                    $subCatIds2 = explode(',',$subCats2);
                                    if(count($subCatIds2) > 1):?>
                                        <ul class="menubox mCustomScrollbar _mCS_1 mCS_no_scrollbar">
                                                <?php foreach($subCatIds2 as $subCat3) :?>
                                                    <li>
                                                    <?php
                                                    $category4 = Mage::getModel('catalog/category')->getCollection();
                                                    $category4->addAttributeToFilter("Top_Menu" , array('eq' => '146'));
                                                    $category4->addAttributeToFilter("entity_id" , array('eq' => $subCat3));
                                                    $category4->addAttributeToSelect('*');
                                                    $category4->load();     
                                                    foreach($category4 as $category5):                                                  
                                                    echo '<a href="'.$category5->getUrl().'">'.$category5->getName()."</a>";?>
                                                    <?php endforeach; ?>
                                                    </li>
                                                <?php endforeach; ?>
                                        </ul>
                                    <?php endif; ?>

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

how this has 700 hits to the database and is makaking my site very slow. How do I reduce the number of loads?


  • Have you created your own block class for this phtml file ? – Danny Dev Nz Nov 17 '14 at 3:04
  • for the menu no. I'm a newbee. – nikhil carvalho Nov 17 '14 at 3:05
  • Sorry yes I have created the block.<block type="catalog/navigation" name="catalog.category" template="catalog/navigation/category.phtml" /> – nikhil carvalho Nov 17 '14 at 3:16
2
    $menu = array(
        'items' => array(),
        'parents' => array()
    );
    $category = Mage::getModel('catalog/category')->getCollection();
    $category->addAttributeToFilter("Top_Menu" , array('eq' => '146'));
    $category->addAttributeToSelect('*');
    $category->setOrder("parent_id", "ASC");
    $categoryArray = $category->toArray();

    $countcat=count($categoryArray['items']);

    for($l=0;$l<$countcat;$l++){
    $idcat=$categoryArray['items'][$l]['entity_id'];
    $parentcat=$categoryArray['items'][$l]['parent_id'];
    $menu['items'][$idcat][] = $categoryArray['items'][$l]['url_path'];
    $menu['items'][$idcat][] = $categoryArray['items'][$l]['name'];
    $menu['items'][$idcat][] = $parentcat;
    $menu['parents'][$parentcat][] = $idcat;
    }



    $parent=2;

$html = "";
               if (isset($menu['parents'][$parent]))
               {
                  $html .= "<ul >\n";
                   foreach ($menu['parents'][$parent] as $itemId)
                   {
                      if(!isset($menu['parents'][$itemId]))
                      {
                         $html .= "<li class='has-sub2'>\n <span class='submenu-button2'></span> <a href='".$menu['items'][$itemId][0]."'><span>".$menu['items'][$itemId][1]."</span></a>\n</li> \n";
                      }
                      if(isset($menu['parents'][$itemId]))
                      {
                         $html .= "<li class='has-sub2'>\n <span class='submenu-button2'></span> <a href='".$menu['items'][$itemId][0]."'><span>".$menu['items'][$itemId][1]."</span></a> \n";
                         //$html .= buildMenu($itemId, $menu);



                                  $html .= "<ul>";
                                   foreach ($menu['parents'][$itemId] as $itemId1)
                                   {
                                      if(!isset($menu['parents'][$itemId1]))
                                      {
                                         $html .= "<li class='has-sub2'>\n  <span class='submenu-button2'></span> <a href='".$menu['items'][$itemId1][0]."'><span>".$menu['items'][$itemId1][1]."</span></a>\n</li> \n";
                                      }
                                      if(isset($menu['parents'][$itemId1]))
                                      {
                                         $html .= "
                                         <li class='has-sub2'>\n  <span class='submenu-button2'></span><a href='".$menu['items'][$itemId1][0]."'><span>".$menu['items'][$itemId1][1]."</span></a> \n";

                                              $html .= "<ul>";
                                               foreach ($menu['parents'][$itemId1] as $itemId2)
                                               {
                                                  if(!isset($menu['parents'][$itemId2]))
                                                  {
                                                     $html .= "<li class='has-sub3'>\n  <span class='submenu-button2'></span><a href='".$menu['items'][$itemId2][0]."'><span>".$menu['items'][$itemId2][1]."</span></a>\n</li> \n";
                                                  }
                                                  if(isset($menu['parents'][$itemId2]))
                                                  {
                                                     $html .= "
                                                     <li class='has-sub3'>\n <span class='submenu-button2'></span> <a href='".$menu['items'][$itemId2][0]."'><span>".$menu['items'][$itemId2][1]."</span></a> \n";

                                                     $html .= "</li> \n";
                                                  }
                                               }
                                               $html .= "</ul>\n";


                                         $html .= "</li> \n";
                                      }
                                   }
                                   $html .= "</ul> \n";

                               $html .= "</li> \n";

                      }
                   }
                   $html .= "</ul> \n";
               }
               echo $html; ?>   

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.