I had the task to create the custom template for the category page. This category page is a page for displaying brands. It had to look like six tabs with names “Top Brands”, “Brands A-D”, “Brands E-J”, “Brands K-P”, “Brands Q-U”, “Brands V-Z”. The tab “Top Brands” had to display images of brands’ logos and the rest of tabs had to display just text links.
I created the custom template and added the template to the block “category.products” in the admin panel. I made a custom block which was inherited from Mage_Catalog_Block_Category_View class.
I used loops for the realization of this idea.
- The first one was a foreach loop where I took each category by its name and put it in the right tab depending on the first category name’s letter.
- The second loop was inside the first foreach for creating ranges – A-D, E-J and etc.
These two loops were inside the block’s class. I had loops in the template, too.
- The first loop in the template was a loop for creating tabs’ headers.
- The second loop was a foreach loop for displaying images in the “Top Brands” tab. The images are taken from the category’s picture.
- The third loop is a loop for displaying links in the rest of the tabs.
- The forth is a loop inside the third loop which divides all links for the parts that contain every 15 links.
It doesn’t work very fast and there was another loop which made the working of this script slower. All categories that should be displayed on the Brands page are children of the Brand category. I decided to take them in the helper. I used a loop foreach where I loaded every category (I don’t think it is the perfect way to do this):
$childCategories = array(); $currentCategory = Mage::registry('current_category'); $currentCategoryChildren = $currentCategory->getChildren(); $currentCategoryChildren = $currentCategoryChildren ? explode(',', $currentCategoryChildren) : ''; if ($currentCategoryChildren) { foreach ($currentCategoryChildren as $childId) { $category = Mage::getModel('catalog/category')->load($childId); if ($category->getIsActive()) { $childCategories[] = array( 'url' => $category->getUrl(), 'name' => $category->getName(), 'img' => $category->getImageUrl() ? $category->getImageUrl() : '' ); } }
And I used the options of Magento’s methods and replaced this awful code with the code below:
$currentCategory = Mage::registry('current_category'); if (!$categoryId && isset($currentCategory)) { $categoryId = $currentCategory->getEntityId(); } $collection = Mage::getResourceModel('catalog/category_collection') ->setStore(Mage::app()->getStore()) ->addAttributeToSelect('name') ->addAttributeToSelect('url_key') ->addAttributeToSelect('image') ->addFieldToFilter('parent_id', $categoryId) ->addFieldToFilter('is_active', 1); if ($sorted) { $collection->addAttributeToSort($sorted); } $categories = $collection->load() ->getItems(); return $categories ? $categories : null;
It works much better.
If there is the better way to solve this task without using so many loops I’ll be glad to read your pieces of advice in the comments to this article 🙂

Certified Magento Developer at Mobecls
4+ years experience
Feel free to ask me anything about this post in the comments below
1 Comment. Leave new
is it possible to get collection of categories and then by single foreach collects all need data into various arrays and then use these array for so many loops as it is need?