JGeer
JGeer

Reputation: 1852

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;?> 

Upvotes: 1

Views: 9383

Answers (6)

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

You need to use different variable inside foreach():-

<?php 
    $items = $_order->getAllItems(); 
    $i = 0; 
    foreach($items as $itm): 
        if($i >= 3) {break;}else{?>
        <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($itm->getProductId())->getSmallImageUrl();?>" border="0" />       </div>
                <div class="order-row-product-name">
                    <?php echo substr($this->escapeHtml($itm->getName()), 0, 20) ?>
                </div>
            </div>
        </li>
<?php $i++; } endforeach;?> 

A much better solution using array_slice():-

<?php 
    $items = $_order->getAllItems(); 
    $item =  array_slice($items, 0, 3); // get first three only
    foreach($item as $itm): 
        <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($itm->getProductId())->getSmallImageUrl();?>" border="0" />       </div>
                <div class="order-row-product-name">
                    <?php echo substr($this->escapeHtml($itm->getName()), 0, 20) ?>
                </div>
            </div>
        </li>
<?php endforeach;?> 

Upvotes: 8

Samuel Ramzan
Samuel Ramzan

Reputation: 1856

    $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 ;)
        }
      }

Upvotes: 0

Accountant م
Accountant م

Reputation: 7483

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;?> 

Upvotes: 1

mehrdadep
mehrdadep

Reputation: 1019

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.

Upvotes: 1

Razon Yang
Razon Yang

Reputation: 454

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; ?>

Upvotes: 1

Xpleria
Xpleria

Reputation: 5853

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
    }
}
?> 

Upvotes: 1

Related Questions