foreach只显示前3项

时间:2017-12-15 10:04:43

标签: php foreach

我们使用foreach代码,我们只想显示前3个项目。 但由于某些原因,我们的代码不起作用,它目前仍显示所有项目。

我在这里缺少什么?

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

6 个答案:

答案 0 :(得分:4)

您需要在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;?> 

使用 array_splice()的更好的解决方案: -

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

答案 1 :(得分:1)

抱歉,读错了。这是更新后的答案。

您的foreach迭代器与计数变量$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
    }
}
?> 

答案 2 :(得分:1)

您正在为循环中的每次迭代重置计数器$i,使用另一个变量$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;?> 

答案 3 :(得分:1)

使用for似乎比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; ?>

答案 4 :(得分:1)

如果您只想展示3件商品,那么您应break foreach之外的其他商品:

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

或简单地使用for循环。

答案 5 :(得分:0)

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