Table of Contents
How to Loop rows with Bootstrap in PHP with examples?
//Get DATA using PDO $query = $conn->prepare('SELECT * FROM table_name WHERE CONDITION ORDER BY RAND() LIMIT 12'); $query->execute(); $result = $query->fetchAll(); //Columns must be a factor of 12 (1,2,3,4,6,12) $numOfCols = 3; $rowCount = 0; $bootstrapColWidth = 12 / $numOfCols; $arrayCount = count($rows); ?> <div class="row"> <?php foreach ($result as $row){ ?> <div class="col-md-<?php echo $bootstrapColWidth; ?>"> <?php echo $row['...']; ?> </div> <?php $rowCount++; if($rowCount % $numOfCols == 0 && $rowCount < $arrayCount) { echo '</div><div class="row">'; } } ?> </div>
or because of Bootstrap works within a 12 col grid system automatically, the code below provides 3 per row automatically since 12 / 4 = 3
<div class="row"> <?php foreach ($rows as $row):?> <div class="col-md-4"> DATA </div> <?php endforeach;?> </div>
Hello there!
I hope you find this post useful!I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.
If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!