首页 > 解决方案 > 如何使用 continue 每 4 次拆分一次

问题描述

如何使用限制 4 明智地设置产品类别这就是为什么我必须每 4 个计数拆分 while 循环

我想要这样的东西:

类别名称 1

类别名称 2

我尝试了以下代码:

$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++;
}

标签: php

解决方案


像这样的东西,所以你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>';
}

推荐阅读