我有一个像下面这样的HTML标记-
<div class="f_product_slider slick">
<div class="row slider_item">
<div class="col-lg-5 col-sm-6">
<div class="item">
<img src="image/bike/cycle_2.png" alt="">
<div class="content text-right">
<a href="product-details.html"><h6>Specialized Sirrus Carbon - 2018</h6></a>
<p>Slayer Bike Expert</p>
</div>
</div>
</div>
<div class="col-lg-5 col-sm-6">
<div class="item item_two">
<img src="image/bike/cycle_2.png" alt="">
<div class="content text-right">
<a href="product-details.html"><h6>Specialized Sirrus Carbon - 2018</h6></a>
<p>Slayer Bike Expert</p>
</div>
</div>
</div>
</div>
<div class="row slider_item">
<div class="col-lg-5 col-sm-6">
<div class="item">
<img src="image/bike/cycle_2.png" alt="">
<div class="content text-right">
<a href="product-details.html"><h6>Specialized Sirrus Carbon - 2018</h6></a>
<p>Slayer Bike Expert</p>
</div>
</div>
</div>
<div class="col-lg-5 col-sm-6">
<div class="item item_two">
<img src="image/bike/cycle_2.png" alt="">
<div class="content text-right">
<a href="product-details.html"><h6>Specialized Sirrus Carbon - 2018</h6></a>
<p>Slayer Bike Expert</p>
</div>
</div>
</div>
</div>
</div>
这里,每行项目包含两个项目。这就是说,在循环的每个步骤中呈现的每两个循环项。 我在这里给出此循环的直观示例-
如何使PHP while循环成为可能? 如果您对这个问题感到困惑,请不要犹豫,向我询问更多详细信息。
答案 0 :(得分:1)
使用for
:
<div class="f_product_slider slick">
<?php for($slideCounter = 0; $slideCounter < 2; $slideCounter++) { ?>
<div class="row slider_item">
<?php for($colCounter = 0; $colCounter < 2; $colCounter++) { ?>
<div class="col-lg-5 col-sm-6">
<div class="item">
<img src="image/bike/cycle_2.png" alt="">
<div class="content text-right">
<a href="product-details.html"><h6>Specialized Sirrus Carbon - 2018</h6></a>
<p>Slayer Bike Expert</p>
</div>
</div>
</div>
<?php } ?>
</div>
<?php } ?>
</div>
使用while
:
<div class="f_product_slider slick">
<?php
$slideCounter = 0;
while($slideCounter < 2) {
$slideCounter++ ?>
<div class="row slider_item">
<?php
$colCounter = 0;
while($colCounter < 2) {
$colCounter++ ?>
<div class="col-lg-5 col-sm-6">
<div class="item">
<img src="image/bike/cycle_2.png" alt="">
<div class="content text-right">
<a href="product-details.html"><h6>Specialized Sirrus Carbon - 2018</h6></a>
<p>Slayer Bike Expert</p>
</div>
</div>
</div>
<?php } ?>
</div>
<?php } ?>
</div>