2

Is it possible to create Bootstrap row for every 3 cols in PHP using foreach loop but also to separate PHP code from View part.

This is my code

<?php include('connect.php'); 
$object = "";

$checkTables = $con -> prepare("SELECT * FROM available");
$checkTables -> execute();
$tables = $checkTables->fetchALL(PDO::FETCH_ASSOC);

$i = 0;

foreach($tables as $table):

    if($table['avail'] == 0) {
        $object .= '<div class="col-sm-4"><div class="table full"><p> 0 seats are avaiable.</p></div></div>';
    } else {
        $object .= '<div class="col-sm-4"><div id="table_'. $table['id'] .'" class="table"><p>' . $table['avail'] . ' seats are avaiable.</p></div></div>';
    }
    $i++;
endforeach;

?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/bootstrap-theme.min.css">
    <link rel="stylesheet" href="css/style.css">
</head>
<body>

<div class="container-fluid text-center">
    <?php echo $object; ?>
</div><!-- End of container -->


<script type="text/javascript" src="js/jquery-2.1.3.min.js"></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</body>
</html>

I would like to keep foreach loop at top of my file like this and separate html and php.

Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176

1 Answers1

1

You can add a new row every time your inner counter modulus 3 equals 0:

$object = '<div class="row">';
foreach($tables as $table):

    if($table['avail'] == 0) {
        $newObject = '<div class="col-sm-4"><div class="table full"><p> 0 seats are avaiable.</p></div></div>';
    } else {
        $newObject = '<div class="col-sm-4"><div id="table_'. $table['id'] .'" class="table"><p>' . $table['avail'] . ' seats are avaiable.</p></div></div>';
    }

    $i++;
    if($i % 3 === 0) {
        // True every time 3 divides into $i evenly
        $newObject = $newObject . '</div><div class="row">';
    }

    $object .= $newObject;
endforeach;
$object .= '</div>';
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • Amazing thank you, i was trying to figure something out from this http://stackoverflow.com/questions/29182704/php-loop-counter-bootstrap-row and this http://stackoverflow.com/questions/20031209/bootstrap-scaffolding-in-phps-loop and others but i just couldn't get it to work. Also do you think this is good practice doing this like this. – Nenad Vracar Nov 02 '15 at 16:24
  • That kinda depends on the scope of your project. if it's small and probably won't change much I don't see a problem, though it is best practice to try to separate out as much as possible the logic from the presentation (your markup). Putting it in a separate file like Gauthier suggested may not be a bad idea if you have no model/controller – Jeff Lambert Nov 02 '15 at 16:28
  • I understand. This is just for learning purposes and later i will transfer to some MVC framework, probably Laravel 5. Thanks again. – Nenad Vracar Nov 02 '15 at 16:35