我希望模板清晰。每3个div然后添加新的div并关闭div。并循环我的数据。
数据循环时我遇到麻烦,然后模板损坏了……
这是我的查询
<?php
$i = 1;
?>
<?php foreach ($produk as $i) : ?>
<?php if ($i%3==0): ?>
<div class="grid1_of_3">
<a href="details.html">
<img src="images/pic6.jpg" alt=""/>
<h3>branded cargos<?= $i['harga'];?></h3>
<div class="price">
<h4>$300<span>indulge</span></h4>
</div>
<span class="b_btm"></span>
</a>
</div>
<?php else : ?>
<div class="grids_of_3">
<div class="grid1_of_3">
<a href="details.html">
<img src="images/pic4.jpg" alt=""/>
<h3>branded bags<?= $i['harga'];?></h3>
<div class="price">
<h4>$300<span>indulge</span></h4>
</div>
<span class="b_btm"></span>
</a>
</div>
<div class="clear"></div>
</div>
<?php endif; ?>
<?php endforeach; ?>
<div class="grids_of_3">
<div class="grid1_of_3">
<a href="details.html">
<img src="images/pic4.jpg" alt=""/>
<h3>branded bags</h3>
<div class="price">
<h4>$300<span>indulge</span></h4>
</div>
<span class="b_btm"></span>
</a>
</div>
<div class="grid1_of_3">
<a href="details.html">
<img src="images/pic5.jpg" alt=""/>
<h3>ems women bag</h3>
<div class="price">
<h4>$300<span>indulge</span></h4>
</div>
<span class="b_btm"></span>
</a>
</div>
<div class="grid1_of_3">
<a href="details.html">
<img src="images/pic6.jpg" alt=""/>
<h3>branded cargos</h3>
<div class="price">
<h4>$300<span>indulge</span></h4>
</div>
<span class="b_btm"></span>
</a>
</div>
<div class="clear"></div>
</div>
我有上述模板,每3个中 然后添加并关闭div
简单的是每3个div类,然后添加1个新的div并关闭div
答案 0 :(得分:0)
您正在将索引与foreach语句中的值混合。如果您有一个数字索引数组(从0开始),请使用:
foreach ($array as $index => $value)
,然后使用以下命令检查每三个元素:
if (($index + 1) % 3 === 0)
答案 1 :(得分:0)
这将为您提供所需的代码
$produk = array(1,2,3,4,5,6,7,8);
$length = sizeof($produk)
<div class="grids_of_3">
<?php foreach ($produk as $index => $i) { ?>
<div class="grid1_of_3">
<a href="details.html">
<img src="images/pic6.jpg" alt=""/>
<h3>branded cargos<?= $i['harga'];?> <?php echo $index ?></h3>
<div class="price">
<h4>$300<span>indulge</span></h4>
</div>
<span class="b_btm"></span>
</a>
</div>
<?php if( ($index + 1) % 3 == 0 && ($index+1) != $length){ ?>
<div class="clear"></div>
</div><div class="grids_of_3">
<?php } else if(($index+1) == $length){ ?>
</div>
<?php } ?>
<?php } ?>
结果代码如下
<div class="grids_of_3">
<div class="grid1_of_3">
<a href="details.html">
<img src="images/pic4.jpg" alt=""/>
<h3>branded bags</h3>
<div class="price">
<h4>$300<span>indulge</span></h4>
</div>
<span class="b_btm"></span>
</a>
</div>
<div class="grid1_of_3">
<a href="details.html">
<img src="images/pic5.jpg" alt=""/>
<h3>ems women bag</h3>
<div class="price">
<h4>$300<span>indulge</span></h4>
</div>
<span class="b_btm"></span>
</a>
</div>
<div class="grid1_of_3">
<a href="details.html">
<img src="images/pic6.jpg" alt=""/>
<h3>branded cargos</h3>
<div class="price">
<h4>$300<span>indulge</span></h4>
</div>
<span class="b_btm"></span>
</a>
</div>
<div class="clear"></div>
</div>