我需要一些关于OpenCart的PHP循环的帮助.
我需要做的是每4个循环包围一个div输出数据.
我有以下内容
<?PHP foreach ($categories as $category) { ?> <div class="col-lg-3 col-md-3">.....</div> <?PHP } ?>
我明白了
<div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div>
但我希望得到的是
<div class="row"> <div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div> </div> <div class="row"> <div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div> </div> <div class="row"> <div class="col-lg-3 col-md-3">.....</div> <div class="col-lg-3 col-md-3">.....</div> </div>
任何帮助将不胜感激 :)
试试这个
原文链接:https://www.f2er.com/php/133261.html<?PHP $i=0; $wrap_count = 4; // you can change this no of divs to wrap foreach ($categories as $category) { $i+=1; if($i%$wrap_count==1) { echo '<div class="row">'; } ?> <div class="col-lg-3 col-md-3">.....</div> <?PHP if($i%$wrap_count==0) { echo '</div>'; } } if($i%$wrap_count!=0) { echo '</div>'; } ?>