php循环计数器引导行
我在 Stack 上看到了很多解决方案,但似乎没有一个对我有用,所以我试图根据我看到的其他人所做的来编写它.
我可以计算列数并开始新行,但问题是,因为我在 foreach 中执行此操作(从 DB 获取数据),它现在会将 DB 中的每个条目放置 4 次-> 关闭该行;开始一个新行-> 并显示 DB 中的第二个条目 4 次-> 关闭该行;开始一个新行->并显示 DB 中的第三个条目 4 次,依此类推...(我的代码在这篇文章的底部)
所以,现在这段代码显示的项目如下:
col-md3(item1) |col-md3(item1) |col-md3(item1) |col-md3(item1)col-md3(item2) |col-md3(item2) |col-md3(item2) |col-md3(item2)........
但是,我希望它显示为:
col-md3(item1) |col-md3(item2) |col-md3(item3) |col-md3(item4)col-md3(item5) |col-md3(item6) |col-md3(item7) |col-md3(item8).... |... |.... |........
我现在的代码:
foreach($data as $row) {echo '<div class="row">';对于 ($i=0; $i<4;$i++){如果 ($i%4 == 0 && $i != 0){echo '</div><div class="row">';}echo '';?><!-- 电子商务 UI #2 --><div class="ecom-ui ecom-ui-two"><div class="img-container"><!-- 产品图片--><a href="#"><img class="img-responsive" src="img/product/<?php echo $row[thumbimage]; ?>"alt=""/></a><!-- 产品详细信息--><div class="product-details"><!-- 产品名称--><h4><a href="#"><?php echo $row[name];?></a><span class="color pull-right">€<?php echo $row[price];?></span></h4><div class="clearfix"></div><p>Lorem Ipsum 只是印刷行业的虚拟文本.</p><!-- 价格--><div><span class="cart"><a href="#">加入购物车</a></span><!-- 媒体图标--><span class="p-media pull-right"><a href="#" class="b-tooltip" data-placement="top" title="21"><i class="fa fa-heart red"></i></a><a href="#" class="b-tooltip" data-placement="top" title="49"><i class="fa fa-share-alt red"></i></a><a href="#" class="b-tooltip" data-placement="top" title="35"><i class="fa fa-thumbs-up red"></i></a></span><div class="clearfix"></div>
<!-- 电子商务 UI #2 --><?php回声'</div>';}回声'</div><br/>';}
解决方案 打印代码存在逻辑错误.您只需要一个循环,而不是嵌套.更改您的代码如下:
';?><!-- 电子商务 UI #2 --><div class="ecom-ui ecom-ui-two"><div class="img-container"><!-- 产品图片--><a href="#"><img class="img-responsive" src="img/product/<?php echo $row[thumbimage]; ?>"alt=""/></a><!-- 产品详情--><div class="product-details"><!-- 产品名称--><h4><a href="#"><?php echo $row[name];?></a><span class="color pull-right">€<?php echo $row[price];?></span></h4><div class="clearfix"></div><p>Lorem Ipsum 只是印刷行业的虚拟文本.</p><!-- 价格--><div><span class="cart"><a href="#">加入购物车</a></span><!-- 媒体图标--><span class="p-media pull-right"><a href="#" class="b-tooltip" data-placement="top" title="21"><i class="fa fa-heart red"></i></a><a href="#" class="b-tooltip" data-placement="top" title="49"><i class="fa fa-share-alt red"></i></a><a href="#" class="b-tooltip" data-placement="top" title="35"><i class="fa fa-thumbs-up red"></i></a></span><div class="clearfix"></div>
<!-- 电子商务 UI #2 -->
<?php$i++;if ($i%4 == 0) echo '</div><div class="row">';?></div><br/>
逻辑: 需要遍历$data
中的所有(每一)行,当循环执行4次时,就断行(by </div>
) 并开始新的(通过
0
启动计数器 $i
并在每次循环后将它增加 1
.当 $i
可以被 4
完全整除时,echo "closed-row(div)-and-starting-row(div)-code",即.
Ive seen alot of solutions here on Stack but none seem to work for me, so im trying to write it based upon what im seeing others do.
Im able to count the columns and start the new row, but the problem is, since im doing this in a foreach (getting data from DB) it will now place each entry from DB 4 times->close the row; start a new row->and show the second entry from DB 4 times->close the row; start a new row->and shows the third entry from DB 4 times, and so on... (my code is on the bottom of this post)
So, right now this code displaying items like:
col-md3(item1) | col-md3(item1) | col-md3(item1) | col-md3(item1)
col-md3(item2) | col-md3(item2) | col-md3(item2) | col-md3(item2)
....
....
however, i want it to display like:
col-md3(item1) | col-md3(item2) | col-md3(item3) | col-md3(item4)
col-md3(item5) | col-md3(item6) | col-md3(item7) | col-md3(item8)
.... | ... | .... | ....
....
My code right now :
foreach($data as $row) {
echo '<div class="row">';
for ($i=0; $i<4;$i++){
if ($i%4 == 0 && $i != 0){
echo '</div><div class="row">';
}
echo '<div class="col-md-3">';
?>
<!-- Ecommerce UI #2 -->
<div class="ecom-ui ecom-ui-two">
<div class="img-container">
<!-- Product Image -->
<a href="#"><img class="img-responsive" src="img/product/<?php echo $row[thumbimage]; ?>" alt="" /></a>
</div>
<!-- product details -->
<div class="product-details">
<!-- product title -->
<h4><a href="#"><?php echo $row[name]; ?></a> <span class="color pull-right">€<?php echo $row[price]; ?></span></h4>
<div class="clearfix"></div>
<p>Lorem Ipsum is simply dummy text of printing the printing industry.</p>
<!-- Price -->
<div>
<span class="cart"><a href="#">Add to cart</a></span>
<!-- Media icon -->
<span class="p-media pull-right">
<a href="#" class="b-tooltip" data-placement="top" title="21"><i class="fa fa-heart red"></i></a>
<a href="#" class="b-tooltip" data-placement="top" title="49"><i class="fa fa-share-alt red"></i></a>
<a href="#" class="b-tooltip" data-placement="top" title="35"><i class="fa fa-thumbs-up red"></i></a>
</span>
<div class="clearfix"></div>
</div>
</div>
</div>
<!-- Ecommerce UI #2 -->
<?php
echo'</div>';
}
echo '</div><br />';
}
解决方案
There is logic error in printing code. You need only one loop, not nested. Change your code as below:
<?php .....
....
$i=0;
echo '<div class="row">';
foreach($data as $row) {
echo '<div class="col-md-3">';
?>
<!-- Ecommerce UI #2 -->
<div class="ecom-ui ecom-ui-two">
<div class="img-container"><!-- Product Image -->
<a href="#"><img class="img-responsive" src="img/product/<?php echo $row[thumbimage]; ?>" alt="" /></a>
</div>
<!-- product details --><div class="product-details">
<!-- product title -->
<h4><a href="#"><?php echo $row[name]; ?></a><span class="color pull-right">€<?php echo $row[price]; ?></span></h4>
<div class="clearfix"></div>
<p>Lorem Ipsum is simply dummy text of printing the printing industry.</p>
<!-- Price --><div>
<span class="cart"><a href="#">Add to cart</a></span>
<!-- Media icon --><span class="p-media pull-right">
<a href="#" class="b-tooltip" data-placement="top" title="21"><i class="fa fa-heart red"></i></a>
<a href="#" class="b-tooltip" data-placement="top" title="49"><i class="fa fa-share-alt red"></i></a>
<a href="#" class="b-tooltip" data-placement="top" title="35"><i class="fa fa-thumbs-up red"></i></a>
</span>
<div class="clearfix"></div>
</div>
</div>
</div>
<!-- Ecommerce UI #2 -->
</div>
<?php
$i++;
if ($i%4 == 0) echo '</div><div class="row">';
} ?>
</div><br />
Logic: Need to loop through all(each) rows in $data
, when loop executed 4 times then break the row(by </div>
) and start the new (by <div class="row">
). To handle that start the counter $i
before loop with 0
and increment it by 1
after each loop. When $i
is fully-dividable by 4
then echo "closing-row(div)-and-starting-row(div)-code", ie. </div><div class="row">
相关文章