...

View Full Version : external var and foreach counting



ubh
02-04-2010, 07:30 PM
I have a very embarrassing question. I am trying to count items in a foreach and then echo out the final number out side of the foreach... yet my number instead of coming out to being 2 comes out to being 12...

Anyone know how I can use an external var out side of a foreach correctly so it can output the correct number?

Seems like 101 stuff but I am stumped. ($itemCount is my bugger)


<?php
Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('checkout/session');
$itemCount = "";
foreach ($session->getQuote()->getAllItems() as $item) {
$cProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$item->getSku());
foreach ($cProduct->getCategoryIds() as $catId){
if($catId == "64"){
$itemCount .= $itemCount + 1;
}
}
}
echo $itemCount;
?>

hivelocitydd
02-04-2010, 07:44 PM
I am not sure I understood your issue .. still try this please




<?php
Mage::getSingleton('core/session', array('name'=>'frontend'));
$session = Mage::getSingleton('checkout/session');
$itemCount = 0;
foreach ($session->getQuote()->getAllItems() as $item) {
$cProduct = Mage::getModel('catalog/product')->loadByAttribute('sku',$item->getSku());
foreach ($cProduct->getCategoryIds() as $catId){
if($catId == "64"){
$itemCount = $itemCount + 1;
}
}
}
echo $itemCount;
?>

ubh
02-04-2010, 07:45 PM
Thx that did it.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum