/    Sign up×
Community /Pin to ProfileBookmark

How do I create an HTML table in PHP

I have 5 pictures stored in a folder and their links stored on the database.
I want to put them in a table of three columns on each row.

[code=php]
<body>
<center>
<table border=’1′>
<?php
$host=””;
$username=””;
$password=””;
$db_name=”fruits_db”;
$tbl_name=”fruits_tbl”;
$connection=mysqli_connect(“$host”,”$username”,”$password”,”$db_name”);
if (mysqli_connect_errno())
{
echo “The application has failed to connect to the mysql database server: ” .mysqli_connect_error();
}
$result = mysqli_query($connection, “SELECT * FROM fruits_tbl”)or die(“Error: ” . mysqli_error($connection));
$num_rows=mysqli_num_rows($result);
$rows = $num_rows/3;

for($i=1; $i<=$rows ; $i++)
{
echo “<tr>”;
for($j=1; $j<=3; $j++)
{
while($row = mysqli_fetch_array($result))
{
echo
(
“<td width=’180px’ height=’200px’>”
.”<div class = ‘fruit_image’>”
.”<img src='”
.$row[‘fruit_image’]
.”‘/>”
.”</div>”
.”<div class = ‘fruit_title’>”
.$row[‘fruit_name’]
.”</div>”
.”</td>”
);
}
}
echo “</tr>”;
}

mysqli_close($connection);
?>
</table>
</center>
</body>
[/code]

The above code I created, contains two FOR loops. The script should count the number of rows in the table, and then divide by 3(the number of columns on each row in the HTML table).
I wonder where I’m going wrong with this code.?

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @Eli_Skywalker spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.1,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...