php - 如何使用 continue 每 4 次拆分一次
问题描述
如何使用限制 4 明智地设置产品类别这就是为什么我必须每 4 个计数拆分 while 循环
我想要这样的东西:
类别名称 1
- 产品1
- 产品 2
- 产品 3
- 产品 4
类别名称 2
- 产品 5
- 产品 6
- 产品 7
- 产品 8
我尝试了以下代码:
$count=0;
while($row_pt = $result_2->fetch_array())
{
if($count%4==0)
{
$output .= '
<div class="col-sm-4 col-lg-3 col-md-3">
<h3>CATEGORY NAME'.$row_pt['cat_id'].'</h3>
<div style="border:1px solid #ccc; border-radius:0px; padding:16px; margin-bottom:16px; height:250px;">
<img src="image/'. $row_pt['product_image'] .'" alt="" class="img-responsive" >
<p align="center"><strong><a href="#">'. $row_pt['product_name'] .'</a></strong></p>
Course Code : '. $row_pt['product_name'] .' <br />
Course Name : '. $row_pt['product_name'] .' <br />
</div>
</div>';
continue;
}
$count++;
}
解决方案
像这样的东西,所以你div
通过使用 count 变量将每四个产品包装在 a 中:
$count = 1;
while ($row_pt = $result_2->fetch_array())
{
if (($count - 1) % 4 === 0)
{
$output .= '<div class="products">';
}
$output .=
'<h3>CATEGORY NAME' . $row_pt['cat_id'] . '</h3>
<div class="product">
<img src="image/' . $row_pt['product_image'] . '">
<a href="#">' . $row_pt['product_name'] . '</a>
<span>Course Code : ' . $row_pt['product_name'] . '</span>
<span>Course Name : ' . $row_pt['product_name'] . '</span>
</div>';
if ($count % 4 === 0)
{
$output .= '</div>';
}
$count++;
}
if (($count - 1) % 4 !== 0)
{
$output .= '</div>';
}
推荐阅读
- google-apps-script - 根据一列中的值整体移动行
- python - Pandas:如何解决“错误标记数据”?
- android-studio - 如何在“trace java methods”模式下使用分析器启动应用程序?
- java - 单元测试中的反射
- symfony - Symfony 断言最小值
- ios - GIT分支后iOS Fabric编译失败
- apache-spark - 在 Spark 丢失记录中爆炸
- javascript - 如何使用 Electron BrowserWindow 和 BrowserView 启用右键单击?
- css - Google Fonts 示例页面和单独页面中的使用之间的 Barlow 渲染差异
- vue.js - Vue JS中每个列表中带有单选按钮的列表时如何绑定(v-model)数据