更改foreach以仅显示第一个/最后一个添加的图像


Change foreach to show only first/last added image

我有以下代码结构。它给出了产品的图像,甚至被添加到购物卡上。图片按添加顺序出现:最上面的图片是最后添加的图片。

我如何改变这段代码,使我得到只显示一个图像,从最近添加的产品(但不是所有的图像从所有添加的产品)?我猜,我需要在foreach ($items as $item)中改变一些东西,这样,图像显示的不是每个$item,而是只有一个$item,但不知道如何修改这个代码…

谢谢你的帮助和建议!

代码如下:

<table>
    <?php $i=0; $k=0; $subtotal = 0;?>
    <?php foreach ($items as $item) : ?>
    <?php
        $link = K2StoreItem::getK2Link($item->product_id);
        $link = JRoute::_($link);
        $image_path = K2StoreItem::getK2Image($item->product_id, $this->params);
    ?>
    <tr class="row<?php echo $k; ?>">
        <?php if($this->params->get('show_thumb_cart')) : ?>
        <td class="warkorb2">
            <?php if(!empty($image_path)) : ?>
            <img src="<?php echo $image_path; ?>" class="itemImg<?php echo $this->params->get('cartimage_size','small') ?>" />
            <?php endif;?>
        </td>
        <?php endif; ?>
    </tr>
    <?php ++$i; $k = (1 - $k); ?>
    <?php endforeach; ?>
<table>

您可以使用带有限制的查询或者做类似的事情

<?php $lastItem = end($items) ?> <!-- return the last item in the $items array -->
<?php
    $link = K2StoreItem::getK2Link($lastItem->product_id);
    $link = JRoute::_($link);
    $image_path = K2StoreItem::getK2Image($lastItem->product_id, $this->params);
?>

在您没有发布的查询中,您可以执行LIMIT 0,1,并添加SORT BY ..如id, time等