将div包装到来自数据库的每3项数据

时间:2015-02-20 17:28:33

标签: php sql foreach

   <div class="accordion-content default">
   <div class="row">
   <div class="col-xs-12 equal">

  <!-- <div >content comming from database</div> -->
  <!-- <div >content comming from database</div> -->
  <!-- <div >content comming from database</div> -->

</div>
<div class="col-xs-12">
  <div class="line-border"> </div>
</div>
</div>
<div class="row">
<div class="col-xs-12 equal">
  <!-- <div >content comming from database</div> -->
  <!-- <div >content comming from database</div> -->
  <!-- <div >content comming from database</div> -->
</div>
<div class="col-xs-12">
  <div class="line-border"> </div>
</div>
</div>
<div class="row">
<div class="col-xs-12 equal">

  <!-- <div >content comming from database</div> -->

  </div>
  <div class="col-xs-12">
  <div class="line-border"> </div>
  </div>
  </div>

  </div>

上面是div的HTML代码,其中来自数据库的内容是来自数据库的内容,我需要每个div显示3个,如果总共有19个那么它应该显示3个每个x 6 = 18和1留下的也应该用

包裹

1 个答案:

答案 0 :(得分:0)

你必须为你的循环声明一个计数器并每隔3个循环重置一次。使用模运算符来执行此操作。

<?php

$i = 0;

foreach($data as $record) {
  if($i == 0) {
    echo '<div class="wrapper">';
  }

  echo $record['some-data'];

  if($i%2 == 0 && $i > 0) {
    echo '</div>';
    $i = 0;
  }

  else {
    $i++;
  }
}