Question : PHP - Modify foreach to display message if 0 iterations

Hi,
I have a code snippet from magento shopping cart.

This foreach loop iterates through the product categories and displays them in a list.

If no categories exist I want to display a message "No categories available"

How could I do this on the current snippet?

Regards
Mitch
1:
2:
3:
4:
5:
6:
7:
8:
<? foreach ($categories as $category) : ?>
        <? $category = Mage::getModel('catalog/category')->load($category->getId()); ?>
        <? if ($current_category && $current_category->getId() == $category->getId()) : ?>
        <li style="color: green"><?= $category->getName(); ?></li>
        <? else : ?>
        <li><a title="<?= $category->getName(); ?>" href="<?= $category->getUrl(); ?>"><?= $category->getName(); ?></a></li>
        <? endif; ?>
    <? endforeach; ?>

Answer : PHP - Modify foreach to display message if 0 iterations

Replace your code with attached code:

Hope this helps
Addy
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
<? 
	$i=0;
foreach ($categories as $category) : ?>
        <? $category = Mage::getModel('catalog/category')->load($category->getId()); ?>
        <? if ($current_category && $current_category->getId() == $category->getId()) : ?>
        <li style="color: green"><?= $category->getName(); ?></li>
        <? else : ?>
        <li><a title="<?= $category->getName(); ?>" href="<?= $category->getUrl(); ?>"><?= $category->getName(); ?></a></li>
        <? endif; ?>
        $i++;
    <? endforeach; ?>
    <?php
    	if($i==0)
		{
			echo 'No Category';
		}
	?>
Random Solutions  
 
programming4us programming4us