Hi all, i wonder is it possible to display my images from database into table with 4 rows and 4 column? How can i do that?

$dbConn = mysqli_connect(DBSERVER, DBUSER, DBPASS, DBNAME) or die(mysqli_connect_error());
$sql = "select * from products";
$result = mysqli_query($dbConn, $sql) or die(mysqli_error($dbConn));

echo "<table border = '1'>";

           while($row = mysqli_fetch_array($result)){
            echo "<tr>
                     <td><img src = 'images/" . $row['img'] . "' width = '200' alt = 'images'></td>
            </tr>";
}
echo "</table>";

Recommended Answers

Use a counter. Start from 0. If the counter modulus 4 is 0 then output a tr tag, then output the td, after that if it is 3 then output a /tr tag. Your fun ends when the counter reaches 15.

Jump to Post

See the modulus operator. If you're still stuck, then see this thread.

Jump to Post

Did you add code to connect to mysql and retrieve results ?

Jump to Post

All 12 Replies

Use a counter. Start from 0. If the counter modulus 4 is 0 then output a tr tag, then output the td, after that if it is 3 then output a /tr tag. Your fun ends when the counter reaches 15.

Hi, i didnt get the expected result.. Is my code wrong?

$dbConn = mysqli_connect(DBSERVER, DBUSER, DBPASS, DBNAME) or die(mysqli_connect_error());
$sql = "select * from hairstyle";
$result = mysqli_query($dbConn, $sql) or die(mysqli_error($dbConn));

echo "<table border = '1'>";
$counter = 0;
           while($row = mysqli_fetch_array($result)){
               if ($counter != 4){
            echo "<tr>
                     <td><img src = 'hairstyle/" . $row['hair_img'] . "' width = '100' alt = 'hairstyle'></td>";
            
            $counter ++;
               }
                   echo "</tr>";
}
echo "</table>";

See the modulus operator. If you're still stuck, then see this thread.

Hi priteas, i would like to thank you first. But however i referred to your codes on the other thread and got this error.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in E:\IISRoot\RP\84557\sally1\viewHairstyle.php on line 8

Did you add code to connect to mysql and retrieve results ?

yes. i did

Then your query may have failed. You may want to post (part of) your code.

$dbConn = mysqli_connect(DBSERVER, DBUSER, DBPASS, DBNAME) or die(mysqli_connect_error());
$sql = "select * from hairstyle";
$result = mysqli_query($dbConn, $sql) or die(mysqli_error($dbConn));
$row = mysql_fetch_array($result);
echo '<table border = "1">';
$i = 0;
while ($row)
{
  if ($i % 5 == 0)
    echo '<tr>';

  echo "<td><img src = 'hairstyle/" . $row['hair_img'] . "' width = '200' alt = 'images'></td>";
  if ($i % 5 == 4)
    echo '</tr>';

  $i++;
}

if ($i % 5 > 0)
{
  $i = 5 - ($i % 5);
  echo "<td colspan='$i'>&nbsp;</td></tr>";
}
echo '</table>';

Here is my code.

Delete line 4 and combine it with line 7 into this:

while ($row = mysqli_fetch_array($result))

Tired before. Same error

Look again, you need to use mysqli functions...

Hey! i solve it. Thanks for that reminder!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.