I am looping through an array of products and for each iteration i call a function theFunction
$products = array (
//...
//...
);
$count = count($products);
$i = 0;
foreach ($products as $product) {
theFunction($id, $name, $i);
$i++;
if($i == $count){
// complete
}
}
Then inside this function i have a couple of other loops with a counter whereby i need to differentiate what happens if it is the first in the loop. To do this I use $counter
and if it is 1 then it processes task 2
otherwise it should always process task 3
function theFunction( $id, $name, $key ){
$design = Mage::getModel('catalog/category')->load($id);
$collection = $design->getProductCollection();
foreach ($collection as $p) {
// do task 1...
// if success/exists then proceed...
if(file_exists('new_name.jpg')) {
$product = Mage::getModel('catalog/product')->load($p->getId());
$new_array = array( $key => $name.'.jpg' );
$counter = 1;
foreach($new_array as $label => $img){
if($counter === 1 ){
// do task 2
}else{
// do task 3
}
$counter++;
}
$product->save();
}
}
}
At the moment the counter is always set at 1 and never increases so it always processes task 2 on each iteration