如何才能使这个foreach行只通过第一个结果?
<?php foreach ($collection as $product) : ?>
我尝试了以下方法,但这不起作用。它不会显示任何结果:
<?php foreach (array_slice($collection, 0, 1) as $product) : ?>
编辑:
以下作品非常完美:
<?php
$i = 0;
foreach ($collection as $product) :
if($i < 1) {
Inner content
}
$i++;
endforeach;
?>
总当前代码:
<tbody>
<tr>
<td class="image" rowspan="3">
<?php $i = 0; foreach ($collection as $product) : if($i < 1) {?>
<img src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail')->resize(75) ?>" alt="<?php echo $this->htmlEscape($product->getName()) ?>" width="75" height="75" />
<?php } $i++; endforeach ?>
</td>
<td class="order" colspan="2">order</td>
<td class="exclTax">excl. vat</td>
<td class="inclTax">incl. vat</td>
</tr>
<?php foreach ($collection as $product) : ?>
<tr>
<td class="select"><input type="radio" name="featured_1807" id="featured_1807_1349567899" value="3071895, IM" data-product-id="3071895" data-product-sup="IM"></td>
<td class="title"><a href="<?php echo $abstractBlock->getProductUrl($product) ?>" class="" tooltip="" title=""><?php echo $this->htmlEscape($product->getName()) ?></a></td>
<td class="price"><?php echo $abstractBlock->getPriceHtml($product, true, '-related') ?></td>
<td class="priceIncl"><?php echo $abstractBlock->getPriceHtml($product, true, '-related') ?></td>
</tr>
<?php endforeach ?>
</tbody>
我怎样才能做到这一点?
答案 0 :(得分:1)
如果您只对第一次出现$collection
您可以改为执行此操作,但可能无需更改循环中当前的任何代码
<?php
$product = $collection[0];
Code you had in the loop
?>
如果它不是数字键,那么你可以使用
<?php
reset($collection); // make sure you are on first occ
$product = $collection[key($collection)];
... html stuff
// and then do again for your second loop
// if you only want the first occ of that as well
reset($collection); // make sure you are on first occ
$product = $collection[key($collection)];