Solved

Foreach loop issue

Posted on 2013-01-13
13
691 Views
Last Modified: 2013-02-05
I currently have a vertical navigation menu the shows parent categories and a mouseover flyout which displays subcategories. I'v managed to show thumbnails and the category description foreach subcategory but I have run into some difficulties.

The foreach loop is duplicating descriptions foreach subcategory and I am not sure why?

I would like to as somebody to have a look at my code as it may be the case that I haven't put the description code in the correct place.
I'd be so grateful if you could please have a look and let me know where I may have gone wrong please...

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
<div class="left-nav-vertical visible-desktop visible-tablet hidden-phone" id="nav-vertical">
<ul class="nav nav-tabs-left" id="dcjq-vertical-mega-menu"> 
<li class="dropdown open visible-desktop visible-tablet hidden-phone" id="left"> 
<a class="dropdown-toggle"><?php echo $this->__('Shop by Department') ?><b class="caret"></b></a> 
<ul class="dropdown-menu">                                 
<ul id="mega" class="menu"> 
<!--Top Category--> 
<?php foreach($_categories as $_category): ?> 
<li id="menu-item-1">
<a href="<?php echo $_helper->getCategoryUrl($_category) ?>"> <?php echo $_category->getName() ?></a>
<ul> 
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
<?php $_subcategories = $_category->getChildrenCategories() ?>
<?php if (count($_subcategories) > 0): ?>
<?php foreach($_subcategories as $_subcategory):$products = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($_subcategory)->addAttributeToSelect('small_image');$products->getSelect()->order(new Zend_Db_Expr('RAND()'))->limit(1);$products->load();?> 
<!-- SubCategory--> 
<ul> 
<li class="media">
<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
<p class="product-name"><?php echo $_subcategory->getName() ?></p> </a> 
<?php foreach($products as $product) { ?>
<?php $cur_category=Mage::getModel('catalog/category')->load($_category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category); ?>
<!-- Show image -->
<!--<a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>" class="pull-left">
<img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(50, 50); ?>" class="static-thumbs" width="50" height="50" alt="<?php echo $_subcategory->getName() ?>" /></a>-->
<!-- End Show image-->
<!-- Show sub category Description -->
<li id="menu-item-4" class="pull-right">
<span><em>

</em></span>
<?php break; } ?>
</li> 
<?php $children = explode( ",", $this->getCurrentCategory()->getChildren()); ?>
        <?php foreach( $children as $child ): ?>
            <?php $_child = Mage::getModel( 'catalog/category' )->load($child) ?>
            <li><?php echo $_child->getDescription(); ?></li>
        <?php endforeach; ?>
<!-- End show category Description -->
</ul> 
<div class="clearer"></div>
<!-- end of subcats -->
<?php endforeach; ?> 
</li> 
<?php endif; ?>  
</ul> 
<!-- end of parent cats -->
<?php endforeach; ?> 
</li> 
</ul> 
</ul> 
</li> 
</ul>
</div> 
<?php endif; ?>
<!--- Closed Navigation Menu -->

<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function(){
  jQuery('#mega').dcVerticalMegaMenu({
    rowItems: '5',
    speed: 'fast',
    effect: 'slide',
    direction: 'right'
  });
});
</script>
<!--- Closed Navigation Menu -->

Open in new window

0
Comment
Question by:rafique12
  • 8
  • 5
13 Comments
 
LVL 11

Expert Comment

by:un1x86
ID: 38773638
Might be a bit easier to debug if you could send some of your html source after it has been generated. Especially the part where it is duplicated.
0
 

Author Comment

by:rafique12
ID: 38773882
Thankyou for your kind response!

The below code is the HTML output from the foreach the duplicate subcategory descriptions are show on lines 1347 and 157. For each subcategory there should only be one description but I'm getting all descriptions foreach subcategory.

Hope this makes sense?!

<ul id="mega" class="menu right"> 
<!--Top Category--> 
 
<li id="menu-item-1" class="dc-mega-li">
<a href="http://localhost:8888/mysite/accessories.html" class="dc-mega"> Accessories<span class="dc-mega-icon"></span></a>
<div class="sub-container mega" style="height: 124px; margin-top: -36px; z-index: 1000; width: 400px; display: none;"><ul class="sub"> 
 

<!-- SubCategory--> 
<ul> 
<li class="media">
<a href="http://localhost:8888/mysite/accessories/x5-mop-accessories.html">
<p class="product-name">HOT Product™ accessories</p> </a> 

<!-- Show image thumbnail -->
<!--<a href="http://localhost:8888/mysite/accessories/x5-mop-accessories.html" class="pull-left">
<img src="http://localhost:8888/mysite/media/catalog/product/cache/1/small_image/50x50/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/small_image.jpg" class="static-thumbs" width="50" height="50" alt="HOT Product™ accessories" /></a>-->
<!-- End Show image-->

<!-- Show sub category Description -->
</li><li id="menu-item-4" class="pull-right">
                        </li><li>Need extra cloths? Spare brushes? Super Absorbent Pads? Here are some fantastic deals on our H2O MOP® X5™ Accessories!</li> ///** DUPLICATE DESCRIPTION **///
         
<!-- End show category Description -->
</ul> 
<!-- end of subcats -->
<div class="clearer"></div>
 
 
  
</ul></div> 
<!-- end of parent cats -->
 
</li><li id="menu-item-1" class="dc-mega-li">
<a href="http://localhost:8888/mysite/bestsellers.html" class="dc-mega"> Bestsellers<span class="dc-mega-icon"></span></a>
<div class="sub-container mega" style="height: 71px; margin-top: -36px; z-index: 1000; width: 400px; display: none;"><ul class="sub"> 
 

<!-- SubCategory--> 
<ul> 
<li class="media">
<a href="http://localhost:8888/mysite/bestsellers/h2o-x5-mop.html">
<p class="product-name">H2O HOT Product</p> </a> 

<!-- Show image thumbnail -->
<!--<a href="http://localhost:8888/mysite/bestsellers/h2o-x5-mop.html" class="pull-left">
<img src="http://localhost:8888/mysite/media/catalog/product/cache/1/small_image/50x50/9df78eab33525d08d6e5fb8d27136e95/h/2/h20_green_copy_1.jpeg" class="static-thumbs" width="50" height="50" alt="H2O HOT Product" /></a>-->
<!-- End Show image-->

<!-- Show sub category Description -->
</li><li id="menu-item-4" class="pull-right">
                        </li><li></li> ///** DUPLICATE DESCRIPTION **///
         
<!-- End show category Description -->
</ul> 
<!-- end of subcats -->
<div class="clearer"></div>
 
 
  
</ul></div> 
<!-- end of parent cats -->
 
</li><li id="menu-item-1" class="dc-mega-li">
<a href="http://localhost:8888/mysite/fitness-beauty.html" class="dc-mega"> Fitness &amp; Beauty<span class="dc-mega-icon"></span></a>
<div class="sub-container mega" style="height: 107px; margin-top: -36px; z-index: 1000; width: 400px; display: none;"><ul class="sub"> 
 

<!-- SubCategory--> 
<ul> 
<li class="media">
<a href="http://localhost:8888/mysite/fitness-beauty/ageless-wonder-21.html">
<p class="product-name">Ageless Wonder</p> </a> 

<!-- Show image thumbnail -->
<!--<a href="http://localhost:8888/mysite/fitness-beauty/ageless-wonder-21.html" class="pull-left">
<img src="http://localhost:8888/mysite/media/catalog/product/cache/1/small_image/50x50/9df78eab33525d08d6e5fb8d27136e95/a/g/ageless_wonder_1_copy.jpeg" class="static-thumbs" width="50" height="50" alt="Ageless Wonder" /></a>-->
<!-- End Show image-->

<!-- Show sub category Description -->
</li><li id="menu-item-4" class="pull-right">
                        </li><li></li> ///** DUPLICATE DESCRIPTION **///
                            <li></li>
         
<!-- End show category Description -->
</ul> 
<!-- end of subcats -->
<div class="clearer"></div>
 

<!-- SubCategory--> 
<ul> 
<li class="media">
<a href="http://localhost:8888/mysite/fitness-beauty/steel-abs-4.html">
<p class="product-name">Steel Abs</p> </a> 

<!-- Show image thumbnail -->
<!--<a href="http://localhost:8888/mysite/fitness-beauty/steel-abs-4.html" class="pull-left">
<img src="http://localhost:8888/mysite/media/catalog/product/cache/1/small_image/50x50/9df78eab33525d08d6e5fb8d27136e95/s/t/steel-abs_2.png" class="static-thumbs" width="50" height="50" alt="Steel Abs" /></a>-->
<!-- End Show image-->

<!-- Show sub category Description -->
</li><li id="menu-item-4" class="pull-right">
                        </li><li></li> ///** DUPLICATE DESCRIPTION **///
                            <li></li>
         
<!-- End show category Description -->
</ul> 
<!-- end of subcats -->
<div class="clearer"></div>
 
 
  
</ul></div> 
<!-- end of parent cats -->
 
</li><li id="menu-item-1" class="dc-mega-li">
<a href="http://localhost:8888/mysite/house-and-home.html" class="dc-mega"> House and Home<span class="dc-mega-icon"></span></a>
<div class="sub-container mega" style="height: 178px; margin-top: -90.29632568359375px; z-index: 1000; width: 400px; display: none; opacity: 1; left: 206px;"><ul class="sub"> 
 

<!-- SubCategory--> 
<ul> 
<li class="media">
<a href="http://localhost:8888/mysite/house-and-home/flavourstone.html">
<p class="product-name">FlavourStone</p> </a> 

<!-- Show image thumbnail -->
<!--<a href="http://localhost:8888/mysite/house-and-home/flavourstone.html" class="pull-left">
<img src="http://localhost:8888/mysite/media/catalog/product/cache/1/small_image/50x50/9df78eab33525d08d6e5fb8d27136e95/s/e/set_3_transform_2_2.jpg" class="static-thumbs" width="50" height="50" alt="FlavourStone" /></a>-->
<!-- End Show image-->

<!-- Show sub category Description -->
</li><li id="menu-item-4" class="pull-right">
                        </li><li>Great for home cooking...</li> ///** DUPLICATE DESCRIPTION **///
                            <li>HOT Product</li>
         
<!-- End show category Description -->
</ul> 
<!-- end of subcats -->
<div class="clearer"></div>
 

<!-- SubCategory--> 
<ul> 
<li class="media">
<a href="http://localhost:8888/mysite/house-and-home/h2o-x5-mop-8.html">
<p class="product-name">H2O HOT Product</p> </a> 

<!-- Show image thumbnail -->
<!--<a href="http://localhost:8888/mysite/house-and-home/h2o-x5-mop-8.html" class="pull-left">
<img src="http://localhost:8888/mysite/media/catalog/product/cache/1/small_image/50x50/9df78eab33525d08d6e5fb8d27136e95/h/2/h20_green_copy_1.jpeg" class="static-thumbs" width="50" height="50" alt="H2O HOT Product" /></a>-->
<!-- End Show image-->

<!-- Show sub category Description -->
</li><li id="menu-item-4" class="pull-right">
                        </li><li>Great for home cooking...</li> ///** DUPLICATE DESCRIPTION **///
                            <li>HOT Product</li>
         
<!-- End show category Description -->
</ul> 
<!-- end of subcats -->
<div class="clearer"></div>
 
 
  
</ul></div> 
<!-- end of parent cats -->
 
</li><li id="menu-item-1" class="media-object">
<a href="http://localhost:8888/mysite/offers.html" class="dc-mega"> Offers<span class="dc-mega-icon"></span></a>
<div class="sub-container non-mega" style="height: 34px; margin-top: -37px; z-index: 1000; display: block; width: 400px; opacity: 1; left: 206px;"><ul class="sub"> 
  
</ul></div> 
<!-- end of parent cats -->
 
</li><li id="menu-item-1">
<a href="http://localhost:8888/mysite/outlet.html" class="dc-mega"> Outlet<span class="dc-mega-icon"></span></a>
<div class="sub-container non-mega" style="height: 34px; margin-top: -36px; z-index: 1000; display: none;"><ul class="sub"> 
  
</ul></div> 
<!-- end of parent cats -->
 
</li> 
</ul>

Open in new window

0
 
LVL 11

Expert Comment

by:un1x86
ID: 38773897
you may want to add a debug print of $children on line 40 of your first code post.

<?php 
  $children = explode( ",", $this->getCurrentCategory()->getChildren()); 
  echo "<pre>";
  print_f($children);
  echo "</pre>";
?>

Open in new window


Show me the print_f output where you get the duplicate.

Note: I still don't see why this is a duplicate?
0
 

Author Comment

by:rafique12
ID: 38773947
Fatal error:  Call to undefined function print_f() in /Applications/MAMP/htdocs/mysite/app/design/frontend/bootstrapped/default/template/vertnav/left.phtml on line 41


HTML if it helps
<ul id="mega" class="menu"> 
<!--Top Category--> 
 
<li id="menu-item-1">
<a href="http://localhost:8888/thanedirect/accessories.html"> Accessories</a>
<ul> 
 

<!-- SubCategory--> 
<ul> 
<li class="media">
<a href="http://localhost:8888/thanedirect/accessories/x5-mop-accessories.html">
<p class="product-name">X5 Mop™ accessories</p> </a> 

<!-- Show image thumbnail -->
<!--<a href="http://localhost:8888/thanedirect/accessories/x5-mop-accessories.html" class="pull-left">
<img src="http://localhost:8888/thanedirect/media/catalog/product/cache/1/small_image/50x50/9df78eab33525d08d6e5fb8d27136e95/t/o/tobi_detail_copy.jpeg" class="static-thumbs" width="50" height="50" alt="X5 Mop™ accessories" /></a>-->
<!-- End Show image-->

<!-- Show sub category Description -->
</li><li id="menu-item-4" class="pull-right">
<pre><br>
<b>Fatal error</b>:  Call to undefined function print_f() in <b>/Applications/MAMP/htdocs/mysite/app/design/frontend/bootstrapped/default/template/vertnav/left.phtml</b> on line <b>41</b><br>
</pre></li></ul></ul></li></ul>

Open in new window


I've attached a screenshot of the navigation. if you see the text in orange is the same for each subcategory...
left-nav.png
0
 
LVL 11

Expert Comment

by:un1x86
ID: 38773957
sorry it should be print_r() of course.

I think that $this->getCurrentCategory()->getChildren() is always the same.
0
 

Author Comment

by:rafique12
ID: 38773968
Good spot!

I get this result:

Array
(
    [0] => 42
    [1] => 51
)

and

Array
(
    [0] => 42
    [1] => 51
)
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 11

Expert Comment

by:un1x86
ID: 38773978
Sorry but I am not familiar with that Magento class. But maybe you can print_r($_category) to see what is in there.

Also look at
$cur_category
0
 

Author Comment

by:rafique12
ID: 38774013
The output is long and contains Mage::getModel instances. I hope this helps!

<pre>Mage_Catalog_Model_Category Object
(
    [_eventPrefix:protected] =&gt; catalog_category
    [_eventObject:protected] =&gt; category
    [_cacheTag:protected] =&gt; catalog_category
    [_useFlatResource:protected] =&gt; 1
    [_designAttributes:Mage_Catalog_Model_Category:private] =&gt; Array
        (
            [0] =&gt; custom_design
            [1] =&gt; custom_design_from
            [2] =&gt; custom_design_to
            [3] =&gt; page_layout
            [4] =&gt; custom_layout_update
            [5] =&gt; custom_apply_to_products
        )

    [_treeModel:protected] =&gt; 
    [_defaultValues:protected] =&gt; Array
        (
        )

    [_storeValuesFlags:protected] =&gt; Array
        (
        )

    [_lockedAttributes:protected] =&gt; Array
        (
        )

    [_isDeleteable:protected] =&gt; 1
    [_isReadonly:protected] =&gt; 
    [_resourceName:protected] =&gt; catalog/category_flat
    [_resource:protected] =&gt; 
    [_resourceCollectionName:protected] =&gt; catalog/category_flat_collection
    [_dataSaveAllowed:protected] =&gt; 1
    [_isObjectNew:protected] =&gt; 
    [_data:protected] =&gt; Array
        (
            [entity_id] =&gt; 37
            [parent_id] =&gt; 3
            [created_at] =&gt; 2012-09-19 12:45:58
            [updated_at] =&gt; 2012-12-21 13:02:19
            [path] =&gt; 1/3/37
            [position] =&gt; 5
            [level] =&gt; 2
            [children_count] =&gt; 2
            [store_id] =&gt; 1
            [ajaxcart] =&gt; 
            [all_children] =&gt; 
            [available_sort_by] =&gt; 
            [children] =&gt; 
            [custom_apply_to_products] =&gt; 0
            [custom_design] =&gt; 
            [custom_design_from] =&gt; 
            [custom_design_to] =&gt; 
            [custom_layout_update] =&gt; <reference name="product_list">
<action method="setColumnCount"><count>4</count></action>
</reference>


            [custom_use_parent_settings] =&gt; 0
            [default_sort_by] =&gt; 
            [description] =&gt; Lorem ipsum this is a test
            [display_mode] =&gt; PRODUCTS
            [filter_price_range] =&gt; 
            [groupscatalog2_groups] =&gt; -2
            [groupscatalog_hide_group] =&gt; 
            [image] =&gt; 
            [include_in_menu] =&gt; 1
            [is_active] =&gt; 1
            [is_anchor] =&gt; 0
            [landing_page] =&gt; 
            [meta_description] =&gt; 
            [meta_keywords] =&gt; 
            [meta_title] =&gt; Thane direct House and Home
            [name] =&gt; House and Home
            [page_layout] =&gt; three_columns
            [path_in_store] =&gt; 
            [thumbnail] =&gt; 
            [url_key] =&gt; house-and-home
            [url_path] =&gt; house-and-home.html
        )

    [_hasDataChanges:protected] =&gt; 
    [_origData:protected] =&gt; Array
        (
            [entity_id] =&gt; 37
            [parent_id] =&gt; 3
            [created_at] =&gt; 2012-09-19 12:45:58
            [updated_at] =&gt; 2012-12-21 13:02:19
            [path] =&gt; 1/3/37
            [position] =&gt; 5
            [level] =&gt; 2
            [children_count] =&gt; 2
            [store_id] =&gt; 1
            [ajaxcart] =&gt; 
            [all_children] =&gt; 
            [available_sort_by] =&gt; 
            [children] =&gt; 
            [custom_apply_to_products] =&gt; 0
            [custom_design] =&gt; 
            [custom_design_from] =&gt; 
            [custom_design_to] =&gt; 
            [custom_layout_update] =&gt; <reference name="product_list">
<action method="setColumnCount"><count>4</count></action>
</reference>


            [custom_use_parent_settings] =&gt; 0
            [default_sort_by] =&gt; 
            [description] =&gt; Lorem ipsum this is a test
            [display_mode] =&gt; PRODUCTS
            [filter_price_range] =&gt; 
            [groupscatalog2_groups] =&gt; -2
            [groupscatalog_hide_group] =&gt; 
            [image] =&gt; 
            [include_in_menu] =&gt; 1
            [is_active] =&gt; 1
            [is_anchor] =&gt; 0
            [landing_page] =&gt; 
            [meta_description] =&gt; 
            [meta_keywords] =&gt; 
            [meta_title] =&gt; Thane direct House and Home
            [name] =&gt; House and Home
            [page_layout] =&gt; three_columns
            [path_in_store] =&gt; 
            [thumbnail] =&gt; 
            [url_key] =&gt; house-and-home
            [url_path] =&gt; house-and-home.html
        )

    [_idFieldName:protected] =&gt; entity_id
    [_isDeleted:protected] =&gt; 
    [_oldFieldsMap:protected] =&gt; Array
        (
        )

    [_syncFieldsMap:protected] =&gt; Array
        (
        )

)
</pre>

Open in new window

0
 
LVL 11

Expert Comment

by:un1x86
ID: 38774074
Not really what I was looking for. Maybe check print_r($_subcategory). Try to find the description in one of the foreach variables..
0
 

Author Comment

by:rafique12
ID: 38774113
Errr

print_r($_subcategory) returns no output

and print_r($_child) returns:

50

then

51

Have we hit a brick wall?
0
 

Author Comment

by:rafique12
ID: 38792900
Still trying to resolve this. Although the descriptions are show they are echoing all descriptions foreach category...
0
 

Accepted Solution

by:
rafique12 earned 0 total points
ID: 38839435
<div class="sub-nav-desc">
  <?php $children = explode( ",", $this->getCurrentCategory()->getChildren()); ?>
  <?php foreach( $children as $child ): ?>
	  <?php $_child = Mage::getModel('catalog/category')->load($_subcategory->getId()); ?>
	  
	  <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
		   <span class="product-name"><?php echo $_subcategory->getName() ?></span>
	  </a> 
		<em><?php $desct = nl2br($this->htmlEscape($_child->getDescription())) ?>
		<?php echo Mage::helper('core/string')->truncate($desct, '100'); ?></em>
	  </div>

Open in new window

0
 

Author Closing Comment

by:rafique12
ID: 38854277
Solved with the code above^

Thankyou to everyone who participated in trying to solve this issue
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
In this tutorial viewers will learn how add a scalable full-width header using CSS3. Create a new HTML document with an internal stylesheet. Set a tiled background.:  Create a new div and name it Header. Position it with position:absolute at the top…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now