Repeating Titles in headers when storing SQL data in table - php

When trying to display a table which has SQL data present, the rows at the top of the table are repeating, which I do not want to happen! I know it is probably something stupid but I've tried for a while to solve this and can't. Images outline the code which I used, and the output which is displayed

Write the first <tr></tr> before while
like this :
echo "<table>";
echo "<tr>
<th>albul Name</th>
..
..
..
</tr>";
while($album=$stmt->fetchObject()) {
echo "<tr>
<td>$album->$album_name
...
...
</tr>";
}

The issue is you're echoing your Header row inside of your WHILE loop, so you're writing the header for each iteration of the loop.
To fix, move the header row out of your loop like this:
echo "<table><tr>
<th>album Name</th>
<th>Year</th>
<th>Genre</th>
<th>Artist Name</th>
<th>Total Running Time</th>
</tr>"
while ($album = $stmt->fetchObject()) {
//Display the data as a row.
echo "<tr>
<td>$album->album_name</td>
<td>$album->year</td>
<td>$album->genre</td>
<td>$album->artist_name</td>
<td>$album->total_time</td>
</tr>"
}//end loop
echo "</table>";

When you're looking at something for too long you just can't see it anymore. Just take your tags out of the while loop. You only need to the data rows in the loop.

Take the code out of the WHILE loop.
The first row and should be before the loop code starts

you need change your code is firts set your struture html table and next php code:
<?php
echo"
<table border="1">
<tr>
<td>ALBUM</td>
<td>YEAR</td>
<td>GENERE</td>
<td>ARTIST</td>
<td>TOTAL PLAYING TIME</td>
</tr>
";
now you need print your records from db
while($album=$stmt->fetchObject()) {
echo "<tr>
<td>$album->$album_name</td>
<td>$album->$year</td>
<td>$album->$genre</td>
<td>$album->$artist_name</td>
<td>$album->$total_time</td>
</tr>";
}//end while
all code is this:
<?php
echo"
<table border="1">
<tr>
<td>ALBUM</td>
<td>YEAR</td>
<td>GENERE</td>
<td>ARTIST</td>
<td>TOTAL PLAYING TIME</td>
</tr>
";
while($album=$stmt->fetchObject()) {
echo "<tr>
<td>$album->$album_name</td>
<td>$album->$year</td>
<td>$album->$genre</td>
<td>$album->$artist_name</td>
<td>$album->$total_time</td>
</tr>";
}//end while

Related

Base number of HTML table rows on number of SQL results

I want to make a table based on a result from an SQL query in PHP. I know that if my query returned 2 results I could use
echo '<table>
<tr>
<th>User</th>
<th>Answer</th>
</tr>
<tr>
<td>$row[0]['username']</td>
<td>$row[0]['answer']</td>
</tr>
<tr>
<td>$row[1]['username']</td>
<td>$row[1]['answer']</td>
</tr>
But how do I go about this if I don't know how many results will be returned
EDIT:
I have used a loop as advised but the results appear underneath the table instead of inside. Only the table headers are inside. Any ideas?
echo '<table class="collabtable">
<tr>
<th>User</th>
<th>Answer</th>
</tr>';
foreach($rows as $row){
if (isset($row['collabans'])){
echo '<tr>
<td>'.$row['username'].'</td>
<td>'.$row['collabans'].'</td>
</tr>';
}
echo '</table>';
}
}
You can use loop. See below.
<table>
<tr>
<th>User</th>
<th>Answer</th>
</tr>
<?php foreach ($rows as $row) {
if (isset($row['collabans'])){ ?>
<tr>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['answer']; ?></td>
</tr>
<?php }
} ?>
</table>
You need to loop through all results of your query result as below:
if ($result->num_rows() > 0) {
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
<tr>
<td>$row['username']</td>
<td>$row['answer']</td>
</tr>
}
}
Hope it helps you.

Error populating html table from MySQL database.

I am trying to populate my html table using data from my database but each a new row is added to the table, the table shifts downwards. If for example, 38 rows is added to the table, the table becomes invisible. You have to scroll down to see the table. What might the problem be?
echo "
<div class='container'>
<span class='cc' style='font-weight: bold;'>Count: $stat</span></br></br>
<table class='col-md-9' border='1'>
<th>Agent's name</th>
<th>Student's name</th>
<th>Student's number</th>
<th>Accommodation type</th>
<th>School</th>
<th>Date & Time</th>
";
foreach($db->query($select) AS $result){
echo "
<tr><td>{$result['agent_name']}</td>
<td>{$result['student_name']}</td>
<td>{$result['student_number']}</td>
<td>{$result['accommodation']}</td>
<td>{$result['school']}</td>
<td>{$result['contact_date']}</td>
</tr>
</br>
";
}
echo "
</table>
</div>
";
You cannot have <th> directly inside <table>. Please wrap them inside <tr>.
You must get the rows from the foreach in order to get the result rows. The one that you are looping is just the stdObject ResultSet. Change your code to:
$result = $db->query($select)
if (mysqli_num_rows($result) > 0)
while (false != ($row = mysqli_fetch_array($result)))
// Loop here.
Yes, and as others said, a <table> tag can contain only <tbody>, <thead>, <tfoot> and <tr>. Nothing else. There's no tag like </br>. It must be <br />. Brush up the basics of HTML.
Two things:
First, you need to wrap your header elements in a row:
<tr> <!-- here -->
<th>Agent's name</th>
<th>Student's name</th>
<th>Student's number</th>
<th>Accommodation type</th>
<th>School</th>
<th>Date & Time</th>
</tr> <!-- and here -->
Second, what on Earth are these?:
</br>
The browser is probably interpreting them as this:
<br />
Which means you're adding a line break inside your table structure with each row. Which is probably invalid (since it's outside the table cell and part of the table itself), but probably also why each row pushes the display down a little further. Remove that from your loop.
As it stands the code is generating invalid table markup, so styling and rendering the table is going to be somewhat undefined and possibly different for each browser.
You need to wrap you <th></th> in <tr></tr> too,
That's indeed the case.
Second of all, I will advice not using echo in this case. Here is what the code looks like the way I would do it:
<div class='container'>
<span class='cc' style='font-weight: bold;'><?php Count: $stat ?></span></br></br>
<table class='col-md-9' border='1'>
<tr>
<th>Agent's name</th>
<th>Student's name</th>
<th>Student's number</th>
<th>Accommodation type</th>
<th>School</th>
<th>Date & Time</th>
</tr>
<?php
foreach($db->query($select) AS $result):
?>
<tr>
<td><?=$result['agent_name']?></td>
<td><?=$result['student_name']?></td>
<td><?=$result['student_number']?></td>
<td><?=$result['accommodation']?></td>
<td><?=$result['school']?></td>
<td><?=$result['contact_date']?></td>
</tr>
</br>
<?php
endif;
?>
</table>
</div>
This would be the code I would prefer because you'll keep the HTML colors in your code editor, which makes it easier for others to debug.
Little explanation:
<?= and ?> are easy to use open and close tags to echo a variable within HTML.

While Loop in php not working properly [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
The While Loop is Only Displaying only Last entry
<table border="5" bgcolor="white" width="300" align="center">
<tr>
<th bgcolor="grey">User ID</th>
<th bgcolor="">User Name</th>
<th bgcolor="grey">User Password</th>
</tr>
<?php
$Load = "select * from sudents";
$fetch = mysql_query($Load);
while ($rows = mysql_fetch_array($fetch))
{
$ID=$rows['id'];
$User=$rows['user'];
$Password=$rows['password'];
}
echo "<tr>
<td>$ID</td>
<td>$User</td>
<td>$Password</td>
</tr>
";
?>
</table>
Your loop isn't printing anything to the page. It's setting values to variables. And it's over-writing those variable every time. So when the loop is done, only the last values are still set.
Then you just echo that last value. Once.
Instead, echo the output inside the loop so you can have one element of output for each loop iteration:
while ($rows = mysql_fetch_array($fetch))
{
$ID=$rows['id'];
$User=$rows['user'];
$Password=$rows['password'];
echo "<tr>
<td>$ID</td>
<td>$User</td>
<td>$Password</td>
</tr>";
}
Note, however, that there are a couple of other things wrong here:
Your code is vulnerable to XSS attacks.
You are displaying user passwords. Never, ever do that. Your system shouldn't even have user passwords in a readable format. User passwords should be obscured using a 1-way hash and should never be retrievable by anybody.
As you have it, you are only printing a table row once, and using the last values of the row. All your loop does is assign values to $ID, $User, and $Password, and each loop pass just overwrites the old values. To fix this, you need to move the echo statements into the body of the loop.
This will let you print the current values over each iteration, instead of only printing the last. Here's the code that will work for what you want.
<table border="5" bgcolor="white" width="300" align="center">
<tr>
<th bgcolor="grey">User ID</th>
<th bgcolor="">User Name</th>
<th bgcolor="grey">User Password</th>
</tr>
<?php
$Load = "select * from sudents";
$fetch = mysql_query($Load);
while ($rows = mysql_fetch_array($fetch))
{
$ID=$rows['id'];
$User=$rows['user'];
$Password=$rows['password'];
echo "<tr>
<td>$ID</td>
<td>$User</td>
<td>$Password</td>
</tr>";
}
?>
</table>
Welcome to Stack Overflow #Cat I see two problems.
First: no echo, var_dump() or print found.
Second: the while loop will overwrite anything as long as it iterates, so in your case i would push to an array with each result found, like this:
<?php
$q = "SELECT id, user, password FROM sudents";
$fetch = mysql_query($q);
$final = [];
while ($row = mysql_fetch_array($fetch)) {
final[] = $row;
}
?>
<table>
<tr>
<th>User ID</th>
<th>User Name</th>
<th>User Password</th>
</tr>
<?php foreach($final as $r) {?>
<tr>
<td><?= $r['id'] ?></td>
<td><?= $r['user'] ?></td>
<td><?= $r['password'] ?></td>
</tr>
<?php } ?>
</table>

MYSQL Output - HTML Table

I have a query to output data within my MYSQL database and would like it to display in a table. With "Name" and "UserID" as the headings... currently the code I have developed, does output the results but instead creates multiple instances of the table and doesn't create new rows.
Please could someone help me achieve my goal?
echo"
<table width='400' border='1'>
<tr>
<td>Name</td>
<td>SystemID</td>
</tr>
<tr>
<td> ".$row['FullName']." </td>
<td> ".$row['UserID']." </td>
</tr>
</table>";
The Table should be defined outside of the loop of the data, you just loop through the data and add a row on every loop :
echo "<table width='400' border='1'> ";
echo "<tr>
<td>Name</td>
<td>SystemID</td>
</tr>";
// Example of while loop :
while ($row = mysql_fetch_array($results)) {
echo" <tr>
<td> ".$row['FullName']." </td>
<td> ".$row['UserID']." </td>
</tr>";
}
echo "</table>";
If the code you have shown is within some kind of while loop that loops through the database results, it should be clearly evident why you get a bunch of one row tables. Your entire table output would happen with each loop execution. What you should be doing is outputting your table opening tag and header row before you start the data retrieval loop. Output only the row with each loop execution, and then output the table closing tag after you exit the loop.
echo"
<table width='400' border='1'>
<tr>
<td>Name</td>
<td>SystemID</td>
</tr>";
while ($row = $result->fetch_assoc()) { // or whatever your retrieval code is
echo "<tr>
<td> ".$row['FullName']." </td>
<td> ".$row['UserID']." </td>
</tr>";
}
echo "</table>";
You need to echo the table open tag and headers then loop through the database results, then echo the table closing tag.
You should put <table width='400' border='1'>
<tr>
<td>Name</td>
<td>SystemID</td>
</tr>
before loop (
<HERE>
while() {
...
}
AND </table> you should put after cycle...
it should look like this:
echo "
<table width='400' border='1'>
<tr>
<td>Name</td>
<td>SystemID</td>
</tr>";
while(....) {
echo "
<tr>
<td> ".$row['FullName']." </td>
<td> ".$row['UserID']." </td>
</tr>
";
}
echo "</table>";
http://www.siteground.com/tutorials/php-mysql/display_table_data.htm
Use this inside the loop instead
echo"
<tr>
<td> ".$row['FullName']." </td>
<td> ".$row['UserID']." </td>
</tr>";
and put the rest of the table wrapping around the loop.
Don't complicate the mission ; you don't need to do loop ; mysql provides option whichi is --html , just use it :
mysql --html -uroot -e "USE mydb;select * from mytable"
if you want to display only : FullName and UserID :
mysql --html -uroot -e "USE mydb;select FullName,UserID from mytable"

Displaying html text in loop only once php

I am having difficulty in this matter, i have like 2 columns with name of URL and Status and i have to post data into this html table in a loop, the problem that i am facing is the column title keeps looping itself again and again but i want it only once...this is my code
<?php
if (preg_match("/found/", $html))
{
echo '<table id="table-2">
<thead>
<tr>
<th>URL</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>'.$get_sites_link.'</td>
<td>Keyword Found</td>
</tr></tbody>
</table>';
$vul_url[] = $get_sites_link1;
$links_duplicate_removed = array_unique($vul_url);
file_put_contents($save_file, implode(PHP_EOL, $links_duplicate_removed));
}
else
{
echo '<table id="table-2">
<thead>
<tr>
<th>URL</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<tr>
<td>'.$get_sites_link1.'</td>
<td>Keyword Not Found</td>
</tr></tbody>
</table>'."</b>"."</br>";
}
}
?>
Any help will be appreciated :)
Well, you should probably take the table opening and the header out of the loop. Something along the lines of:
echo "This is the only time this line will be shown.\n";
for ($i = 0; $i < 5; $i++) {
echo "This line was shown $i times!\n";
}
http://codepad.viper-7.com/Tis4wU
To show a thing many times, you need to iterate it, create a loop with:
for
foreach
while
do ... while
for($i=0; $i <=100; $i++){
echo "It's the $i time that I show it";
end

Categories