1

I want to display 4 images result of a SQL query for each row with Bootstrap. How can I do it with a loop in PHP?

<div class="row">
   <?php 
        $query = "SELECT * FROM table WHERE nombre='x' ORDER BY nombre";
        $consulta = mysql_query($query, $conexion);

        while ($data = mysql_fetch_assoc($consulta)) { ?>

        <div class='col-md-3 portfolio-item'>
            <a href='img/<?php echo $data['imagen']; ?>'>
                <img class='img-responsive' src='img/<?php echo $data['imagen']; ?>' alt='<?php echo $data['nombre']; ?>'>
            </a>
            <div class='panel panel-default'>
                <div class='panel-body'>
                    <?php echo $data['nombre']; ?>
                </div>
            </div> 

        </div> 
    <?php } ?> 
</div>
0

1 Answer 1

2

Close and open a new row -

    <div class="row">
       <?php 
            $query = "SELECT * FROM table WHERE nombre='x' ORDER BY nombre";
            $consulta = mysql_query($query, $conexion);
            $i = 1;
            while ($data = mysql_fetch_assoc($consulta)) { ?>

            <div class='col-md-3 portfolio-item'>
                <a href='img/<?php echo $data['imagen']; ?>'>
                    <img class='img-responsive' src='img/<?php echo $data['imagen']; ?>' alt='<?php echo $data['nombre']; ?>'>
                </a>
                <div class='panel panel-default'>
                    <div class='panel-body'>
                        <?php echo $data['nombre']; ?>
                    </div>
                </div> 

            </div> 
        <?php 
            if($i % 4 === 0) echo "</div><div class='row'>"; // close and open a div with class row

            $i++; // increment
        } 
        ?> 
    </div>
Sign up to request clarification or add additional context in comments.

Comments

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.