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.

1 Answer 1

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>';
Sign up to request clarification or add additional context in comments.

3 Comments

Amazing thank you, i was trying to figure something out from this stackoverflow.com/questions/29182704/… and this stackoverflow.com/questions/20031209/… and others but i just couldn't get it to work. Also do you think this is good practice doing this like this.
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
I understand. This is just for learning purposes and later i will transfer to some MVC framework, probably Laravel 5. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.