Reputation: 33
I would like to display my values from my database in a table. I've successfully managed to display the values below. But the tables are all separate from one another.
How do I combine them all into one table please?
<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '', 'yourspace');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM profiles");
while ($row = mysqli_fetch_array($result)) {
echo "<div style='border:solid 1px #ccc;padding:10px;float:left;margin-top:10px;'>";
echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th>
<th>Age</th>
<th>gender</th>
<th>hometown</th>
<th>University</th>
<th>Occupation</th> </tr>";
echo "<tr>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['hometown'] . "</td>";
echo "<td>" . $row['university'] . "</td>";
echo "<td>" . $row['occupation'] . "</td>";
echo "</tr>";
echo "</table>";
echo "</div>";
}
mysqli_close($con);
?>
</body>
</html>
Upvotes: 0
Views: 420
Reputation: 81
while ($row = mysqli_fetch_array($result)) {
echo "<div style='border:solid 1px
#ccc;padding:10px;float:left;margin-top:10px;'>";
echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th>
<th>Age</th>
<th>gender</th>
<th>hometown</th>
<th>University</th>
<th>Occupation</th> </tr>";
This is your problem. While loop is running and from every time it runs it makes new table so you need to put this code outside of while loop, just before it. Inside while loop you just need to have code to add new columns in your existing table..
Upvotes: 0
Reputation: 1244
You are not performing the loop correctly. Infact for each record in your profile table you are creating <table>
tags which is not good.
The correct way to do this is like this:
<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '', 'yourspace');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit(); //Exit not to display the table since there is an mysqli error
}
echo "<div style='border:solid 1px #ccc;padding:10px;float:left;margin-top:10px;'>
<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th>
<th>Age</th>
<th>gender</th>
<th>hometown</th>
<th>University</th>
<th>Occupation</th> </tr>
<tr>";
$result = mysqli_query($con, "SELECT * FROM profiles");
while ($row = mysqli_fetch_array($result)) {
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['hometown'] . "</td>";
echo "<td>" . $row['university'] . "</td>";
echo "<td>" . $row['occupation'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
mysqli_close($con);
?>
</body>
</html>
Hope that helps.
Upvotes: 0
Reputation: 94662
First engage brain, then start coding.
You only want the table rows to be output as part of the loop.
<html>
<head>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '', 'yourspace');
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM profiles");
echo "<div style='border:solid 1px #ccc;padding:10px;float:left;margin-top:10px;'>";
echo "<table border='1'>";
echo "<tr> <th>Firstname</th>
<th>Lastname</th> <th>Age</th>
<th>gender</th>
<th>hometown</th>
<th>University</th>
<th>Occupation</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "<td>" . $row['hometown'] . "</td>";
echo "<td>" . $row['university'] . "</td>";
echo "<td>" . $row['occupation'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
mysqli_close($con);
?>
</body>
</html>
Upvotes: 1
Reputation: 12391
keep the table creation and ending part of table out of the while loop like this:
echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th>
<th>Age</th>
<th>gender</th>
<th>hometown</th>
<th>University</th>
<th>Occupation</th> </tr>";
while ($row = mysqli_fetch_array($result)) {....
// print the records <Tr> <Td......
}
echo "</table>";
Upvotes: 0