Search code examples
phphtmlgallery

Php Gallery table


I need to make a table with preferable 4 - 5 pictures per row but instead it continues across my page. Any help would be appreciated

<?php
$host = "localhost";

$user = "root";
$password = "";
$database = "imageupload";

$query = "SELECT id, url, name from images ";

$connect = mysqli_connect($host,$user,$password,$database) or die("Problem connecting.");
$result = mysqli_query($connect,$query) or die("Bad Query.");

mysqli_close($connect);

while($row = $result->fetch_array())
{

    echo "<td>";

    echo "<td><h2><img src=" . $row['url'] . " width=150 height=150/></h2></td>";


    echo "<td><h2>" .$row['name'] . "</h2></td>";


    echo "</td>";
}
?>

<table>

Solution

  • You can try this:

    echo "<table><tr>";
    foreach($row = $result->fetch_array()) {
        if($row['id'] % 4 == 0) { //Change it to 5 if you want 5 images per row
            echo "</tr><tr>";
        }
        echo "<td><img src='" . $row['url'] . "' /></td>";
    }
    echo "</tr></table>";