'foreach display only first 3 items

We use a foreach code and we want to display only the first 3 items. But for some reason our code does not work, it currently still display all items.

What am I missing here?

CODE:

<?php $items = $_order->getAllItems(); $i = 0; foreach($items as $i): if($i < 3) {?>
    <li class="order-row-item">
        <div class="order-row-product">
            <div class="order-row-product-image">
                <img src="<?php echo $_product = Mage::getModel('catalog/product')->load($i->getProductId())->getSmallImageUrl();?>" border="0" />       </div>
            <div class="order-row-product-name">
                <?php echo substr($this->escapeHtml($i->getName()), 0, 20) ?>
            </div>
        </div>
    </li>
<?php $i++; } endforeach;?> 


Solution 1:[1]

Sorry, read the question wrong. Here's the updated answer.

Your foreach iterater was same as the count variable $i

<?php 
$items = $_order->getAllItems(); 
$i = 0;
foreach($items as $item) {
?>
    <li class="order-row-item">
        <div class="order-row-product">
            <div class="order-row-product-image">
                <img src="<?php echo $_product = Mage::getModel('catalog/product')->load($i->getProductId())->getSmallImageUrl();?>" border="0" />       </div>
            <div class="order-row-product-name">
                <?php echo substr($this->escapeHtml($i->getName()), 0, 20) ?>
            </div>
        </div>
    </li>
<?php
    $i++;
    if($i == 3) {
         break; // because we don't want to continue the loop
    }
}
?> 

Solution 2:[2]

Use for seems like more pretty than foreach:

<?php $items = $_order->getAllItems();
for ($i = 0; $i < count($items) && $i < 3; $i++): ?>
    <li class="order-row-item">
        <div class="order-row-product">
            <div class="order-row-product-image">
                <img src="<?php echo $_product = Mage::getModel('catalog/product')->load($items[$i]->getProductId())->getSmallImageUrl(); ?>"
                     border="0"/></div>
            <div class="order-row-product-name">
                <?php echo substr($this->escapeHtml($items[$i]->getName()), 0, 20) ?>
            </div>
        </div>
    </li>
<?php endfor; ?>

Solution 3:[3]

if you want to show only 3 items then you should break out of foreach:

 if($counter >= 3) break;
 else { //rest of the code ...
 }

or simply use a for loop instead.

Solution 4:[4]

you are resetting your counter $i for every iteration in the loop, use another variable $counter

<?php $items = $_order->getAllItems(); $counter = 0; foreach($items as $i): if($counter < 3) {?>
    <li class="order-row-item">
        <div class="order-row-product">
            <div class="order-row-product-image">
                <img src="<?php echo $_product = Mage::getModel('catalog/product')->load($i->getProductId())->getSmallImageUrl();?>" border="0" /></div>
            <div class="order-row-product-name">
                <?php echo substr($this->escapeHtml($i->getName()), 0, 20) ?>
            </div>
        </div>
    </li>
<?php $counter++; } endforeach;?> 

Solution 5:[5]

    $count='1';
      for ( $i = 0; $i <= 100; $i++ ) {
        if ( $var[$i] == "value" ) {
          print $i.'-'.$var[$i] . "<br>"; // print $i to display row number
          if ( $count++ >= 3 ) {
            break;
          }else{
           // have a cup of coffee ;)
        }
      }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Razon Yang
Solution 3 MehrdadEP
Solution 4 Anant Kumar Singh
Solution 5 Samuel Ramzan