PHP: Fetch 6 results on 1 line then fetch next 6 on 2nd line

The aim is to fetch all the mechanics stored in a database and put them on one line until 6 are displayed and then begin a new line.

    <?php
//MySqli Select Query

$mechanics = $mysqli->query("SELECT * FROM mechanics");
while($row = $mechanics->fetch_assoc()) {
    $name = $row['name'];
    $initial = $row['initial'];

echo'




                 <div class="row text-center ">
                  <div class="col-lg-2 col-md-2 col-sm-2 col-xs-6">
                      <div class="div-square">
                           <a href="blank.html" >
 <h3>'.$initial.'</h3>
                      <h4>'.$name.'</h4>
                      </a>
                      </div>
                     </div>

                  </div> ';
}

 ?>

Upvotes: 0

Views: 23

Answers (1)

Don&#39;t Panic
Don&#39;t Panic

Reputation: 41820

It looks like you're using the bootstrap grid, but you're currently printing each record in its own row. You can do it like this instead:

Open a row

echo '<div class="row text-center ">';

Then output the query results, each in a column

while ($row = $mechanics->fetch_assoc()) {
    $name = $row['name'];
    $initial = $row['initial'];
    echo '<div class="col-sm-2 col-xs-6">
        <div class="div-square">
            <a href="blank.html" >
                <h3>'.$initial.'</h3>
                <h4>'.$name.'</h4>
            </a>
        </div>
    </div>';
}

Close the row.

echo '</div>';

Incidentally, you don't need col-lg-2 col-md-2 col-sm-2 If you've defined the column as col-sm-2 that 2 will apply to the larger sizes as well.

Upvotes: 1

Related Questions