简体   繁体   中英

create new row after every 3 column in loop

So what i want to do is want to create a new row after every 3 column printing in php with bootstrap column with foreach loop here is my code

<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>

The code to print row should be inside foreach loop in a specific condition. And the condition for printing row should be as:

<?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>';
    }
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM