【发布时间】:2011-04-15 13:57:55
【问题描述】:
我有一个 foreach 循环,它为我构建了一个产品页面,基本上它把我的产品放在 3 行中。
查看代码:
foreach ($product_sets as $product)
{
$currentRow = ceil($currentItem / 3);
$currentColumn = $currentItem - (($currentRow - 1) * 3);
if ($number_of_blanks == 2) :
if (($number_of_rows > 1 && $currentRow == ($number_of_rows - 1) && $currentColumn == 2) || ($number_of_rows == 1 && $currentColumn == 1)) :
?>
<li><img src="<?php echo site_url('assets/img/blocks/guarantee.png'); ?>" alt="5 Year Guarantee" width="242" height="156"></li>
<?php
$currentItem++;
endif;
endif;
?>
<li class="<?php if($currentItem % 3 == 0) echo 'endHomeBlock';?>">
<?php $this->load->view('blocks/product_small', array('product' => $product)); ?>
</li>
<?php
$currentItem++;
}
我想要做的是在第一行的末尾放置一个图像(一个销售点),并随机通过其他行,但保持 3 个项目(包括一个销售点的图像)一排。我有一个名为 images 的数组中的图像路径,看起来与此类似,
$images = array(
'iamge1.png',
'image2.png,
'image3.png,
'image4.png,
);
我怎样才能做到这一点?我已经四处乱跑了几个小时了:(
【问题讨论】:
-
所以你总是每行有三个项目?但是要在结果中随机添加图片(第一行除外)?
-
是的,你完美地总结了我想要的东西!
-
可能没有什么关系,但你上面的示例数组,只有第一项被正确引用。
标签: php codeigniter loops foreach