0

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>";
2
Contributors
12
Replies
13
Views
6 Years
Discussion Span
Last Post by lf.gene
0

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.

Edited by pritaeas: n/a

0

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>";
0

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

0

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

Edited by pritaeas: n/a

0
$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.

0

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

while ($row = mysqli_fetch_array($result))

Edited by pritaeas: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.