PHP foreach 循环帮助
希望有人可以帮助我,
我的网站上有一系列图像,看起来与此类似,
$usp = array('/images/usp1.jpg',
'/images/usp2.jpg',
'/images/usp3.jpg',
'/images/usp4.jpg',
'/images/usp5.jpg',
'/images/usp6.jpg'
);
我有一个页面循环浏览我的产品,并在 li
中显示产品图像和一些详细信息。我想要做的是从 $usp
数组中随机选择开始循环,然后显示 4 个产品,然后从 $usp
数组中随机选择另一个产品,然后显示另外 4 个产品,然后显示从 $usp
数组中随机选择的另一个产品。
本质上我想要这种效果
USP PRODUCT PRODUCT
PRODUCT PRODUCT USP
PRODUCT PRODUCT PRODUCT
PRODUCT USP PRODUCT
PRODUCT PRODUCT PRODUCT
USP PRODUCT PRODUCT
目前这是我的循环。
<?php if(count($product_sets) >= 1) : ?>
<div class="clear clearfix productWrap" id="homeBestSellers">
<!-- <h3 class="label"><?php echo $category_details->categoryTitle; ?> <br> Product Sets</h3> -->
<ul class="clear clearfix productBoxes">
<?php
$number_of_blanks = (3 - (count($product_sets) % 3)); // 0, 1 or 2.
if ($number_of_blanks == 3) :
$number_of_blanks = 0;
endif;
$number_of_rows = ceil(count($product_sets) / 3);
$currentItem = 1;
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++;
}
if ($number_of_blanks > 0) :
?>
<li><img src="<?php echo site_url('assets/img/blocks/phone-number.png'); ?>" alt="Phone Number" width="242" height="156"></li>
<?php
endif;
?>
</ul><!-- #productSets -->
</div><!-- #productWrap -->
<?php endif; ?>
我该如何修改它才能达到预期的效果?
Hoping someone can help me,
I have array of images on my website, it looks similar to this,
$usp = array('/images/usp1.jpg',
'/images/usp2.jpg',
'/images/usp3.jpg',
'/images/usp4.jpg',
'/images/usp5.jpg',
'/images/usp6.jpg'
);
I have a page the loops through my products and displays a product image and some details in an li
. What I am wanting to do it start the loop off with a random selection from the $usp
array and then show 4 products, then place another random selection from the $usp
array, then show another 4 products and then show another random selection from the $usp
array.
In essence I want this effect
USP PRODUCT PRODUCT
PRODUCT PRODUCT USP
PRODUCT PRODUCT PRODUCT
PRODUCT USP PRODUCT
PRODUCT PRODUCT PRODUCT
USP PRODUCT PRODUCT
Currently this is the loop I have.
<?php if(count($product_sets) >= 1) : ?>
<div class="clear clearfix productWrap" id="homeBestSellers">
<!-- <h3 class="label"><?php echo $category_details->categoryTitle; ?> <br> Product Sets</h3> -->
<ul class="clear clearfix productBoxes">
<?php
$number_of_blanks = (3 - (count($product_sets) % 3)); // 0, 1 or 2.
if ($number_of_blanks == 3) :
$number_of_blanks = 0;
endif;
$number_of_rows = ceil(count($product_sets) / 3);
$currentItem = 1;
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++;
}
if ($number_of_blanks > 0) :
?>
<li><img src="<?php echo site_url('assets/img/blocks/phone-number.png'); ?>" alt="Phone Number" width="242" height="156"></li>
<?php
endif;
?>
</ul><!-- #productSets -->
</div><!-- #productWrap -->
<?php endif; ?>
How can I modify this so that I have the desired effect?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在循环顶部插入(粗略概念,可能需要一些修改以适合您的代码):
编辑:正如@Ben所指出的,您可能希望从
$usp 当它们被使用时,或者跟踪哪些已经被退回,这样如果这是一个潜在的问题,同一个项目就不会被多次退回。
Insert at top of loop (rough concept, may need some modification to fit your code):
Edit: As pointed out by @Ben, you may want to remove items from
$usp
as they are used, or keep track of which ones have already been returned, so that the same item isn't returned multiple times if that's a potential problem.这假设
$product_sets
是一个连续数字索引的数组。否则,请使用单独的$i = 0
,每轮递增。This assumes
$product_sets
is a continuously numerically indexed array. Otherwise use a separate$i = 0
that you increment each turn.如果您不希望任何 $usp 项目出现两次,您应该在 foreach 循环之前执行此操作:
然后在 foreach 循环顶部附近执行此操作:
If you don't want any $usp items from appearing twice you should do this before your foreach loop:
And then near the top of your foreach loop: