how to make 4 by 4 table?
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>";
lf.gene
Junior Poster in Training
64 posts since Jun 2010
Reputation Points: 10
Solved Threads: 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.
pritaeas
Posting Expert
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
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>";
lf.gene
Junior Poster in Training
64 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
pritaeas
Posting Expert
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
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
lf.gene
Junior Poster in Training
64 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
Did you add code to connect to mysql and retrieve results ?
pritaeas
Posting Expert
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
lf.gene
Junior Poster in Training
64 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
Then your query may have failed. You may want to post (part of) your code.
pritaeas
Posting Expert
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
$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'> </td></tr>";
}
echo '</table>';
Here is my code.
lf.gene
Junior Poster in Training
64 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
Delete line 4 and combine it with line 7 into this:
while ($row = mysqli_fetch_array($result))
pritaeas
Posting Expert
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
lf.gene
Junior Poster in Training
64 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0
Look again, you need to use mysqli functions...
pritaeas
Posting Expert
5,484 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
Hey! i solve it. Thanks for that reminder!
lf.gene
Junior Poster in Training
64 posts since Jun 2010
Reputation Points: 10
Solved Threads: 0