在循环的每三列之后创建一个新行

时间:2018-07-23 04:36:24

标签: php twitter-bootstrap twitter-bootstrap-3

所以我想做的是在PHP中每3列打印一次,并用foreach循环的bootstrap列创建新行,这是我的代码

<div class = "row">
<?php   foreach($location_list as $location) 
     {

  echo "<div class ='col-md-4'>
          <hr> $location->address </hr>
           <hr> $location->name </hr>
           <hr> $location->pin </hr>
       </div> ";
}

  ?>
</div>

1 个答案:

答案 0 :(得分:1)

在特定条件下,要打印的代码row应该在foreach循环内。并且打印行的条件应为:

<?php
foreach ($location_list as $key => $location) {
    if ($key % 3 == 0) {
        echo '<div class = "row">';
    }

    echo "<div class ='col-md-4'>
          <hr> $location->address </hr>
           <hr> $location->name </hr>
           <hr> $location->pin </hr>
       </div> ";

    if ($key % 3 == 2) {
        echo '</div>';
    }
}
?>