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>
Related
This question already has answers here:
How can I use mysqli_fetch_array() twice?
(4 answers)
Closed 1 year ago.
I have two table. One is an article list and the other one is a category list. In the category table, I have use fetch_assoc() to get the data from my DB and list in the table. But I want to fetch the article's data in the article table. How do I use fetch_assoc() twice in the same php file?
<table>
<caption class="table_title">Category Management</caption>
<thead>
<tr class="table_header">
<th>ID</th>
<th>Ttile</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php while($row = $categoryResult->fetch_assoc()) {?>
<tr class="table_content">
<td><?php echo escape($row['id'])?></td>
<td><?php echo escape($row['title'])?></td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php }?>
</tbody>
</table>
articles table
<tbody>
<?php while($row = $result->fetch_assoc()) {?>
<tr class="table_content">
<td></td>
<td></td>
<td></td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php }?>
</tbody>
Replace
while($row = $categoryResult->fetch_assoc()) {
with
foreach($categoryResult as $row)
It does the same but the foreach approach uses an automatic iterator that will always start from the beginning of the result set.
$categoryResult = $mysqli->query();
// OR
$categoryResult = $stmt->get_result();
// from start to end as associative array
foreach($categoryResult as $row) {
// ...
}
// again, from start to end as associative array
foreach($categoryResult as $row) {
// ...
}
If for some reason, you must use while loop and the manual approach then you need to ensure that you always reset the internal pointer to the first records before starting your loop. However, manual looping is not recommended. It is easier to make more errors when doing this manually with while
$categoryResult->data_seek(0);
while($row = $categoryResult->fetch_assoc()) {
// ...
}
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
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
Can anyone help me how to display table inside PHP if statement.
Here's my code:
if( empty($errors))
{
ACTIVITIES \n
echo ' <table>
<thead>
<tr>
<th>Activity</th>
<th>Price</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Chicken Feeding</td>
<td>$price_chicken</td>
<td>$num_chicken</td>
<td>$total_chicken</td>
</tr>
</tbody>
<tbody>
<tr>
<td>Fish Feeding</td>
<td>$price_fish</td>
<td>$num_fish</td>
<td>$total_fish</td>
</tr>
</tbody>
.................
</table> ';
}
try this
<?php if( empty($errors)): ?>
ACTIVITIES \n
<table>
<thead>
<tr>
<th>Activity</th>
<th>Price</th>
<th>Quantity</th>
<th>Amount</th>
</tr>
</thead>
<tbody>
<tr>
<td>Chicken Feeding</td>
<td><?=$price_chicken;?></td>
<td><?=$num_chicken;?></td>
<td><?=$total_chicken;?></td>
</tr>
</tbody>
<tbody>
<tr>
<td>Fish Feeding</td>
<td><?=$price_fish;?></td>
<td><?=$num_fish;?></td>
<td><?=$total_fish;?></td>
</tr>
</tbody>
.................
</table>
<?php endif; ?>
You have the table code wrapped in single quotes, but there are PHP variables inside. PHP will not translate those variables unless you wrap the table in double quotes.
If this doesn't help, give us an idea of what the output looks like.
Try using a 2-dimensional array and looping through it to create a proper HTML table. This way, there's no hard-coding, so you can make as many rows/columns as you want. Here's the code, just change the array:
<table>
<tbody>
<?php
$tableArray = [["Chicken Feeding", $price_chicken, $num_chicken],
["Fish Feeding", $price_fish, $num_fish]];
foreach ($tableArray as $tableRow) {
echo "<tr>";
foreach ($tableRow as $tableCell) {
echo "<td>$tableCell</td>";
}
echo "</tr>";
}
?>
</tbody>
</table>
Here's the thing. I'm trying to sort my Comics collection. So far, I have two tables, one for the titles and type of comics (ID, prefix, title, type) and another one with individual information for each issue (ID, title, volume, issue number, value).
First thing I do is associate the title from the issues with the appropriate titles from the title table.
I do something like that:
SELECT * FROM comics, comicstitles WHERE comics.comTitle = comicstitles.titID ORDER BY comicstitles.titTitle ASC
What I get is a neat bunch of rows that I organize, and I get results like:
<table class='simple-list'>
<thead>
<tr>
<th>Title</th>
<th>Volume</th>
<th>Issue</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Alpha Flight</td>
<td>1</td>
<td>1</td>
<td>$4.00</td>
</tr>
<tr>
<td>Alpha Flight</td>
<td>1</td>
<td>2</td>
<td>$3.00</td>
</tr>
<tr>
<td>Machine Man</td>
<td>2</td>
<td>1</td>
<td>$2.00</td>
</tr>
<tr>
<td>Wolverine</td>
<td>1</td>
<td>2</td>
<td>$35.00</td>
</tr>
<tr>
<td>Wolverine</td>
<td>1</td>
<td>1</td>
<td>$60.00</td>
</tr>
<tr>
<td>The X-Men</td>
<td>1</td>
<td>115</td>
<td>$100.00</td>
</tr>
<tr>
<td>The X-Men</td>
<td>1</td>
<td>116</td>
<td>$100.00</td>
</tr>
<tr>
<td>The X-Men</td>
<td>1</td>
<td>111</td>
<td>$100.00</td>
</tr>
<tr>
<td>The X-Men</td>
<td>1</td>
<td>114</td>
<td>$100.00</td>
</tr>
<tr>
<td>The X-Men</td>
<td>1</td>
<td>109</td>
<td>$160.00</td>
</tr>
<tr>
<td>The X-Men</td>
<td>1</td>
<td>112</td>
<td>$100.00</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan='4'><h3>Total: $764.00</h3></td>
</tr>
</tfoot>
Now, what I would like to do is a foreach, which would give me something like:
For each title, print the title once and all the individual issues underneath.
I would end up with something like:
TITLE
Issue number
Cover
Issue number
Cover
TITLE
Issue number
Cover
TITLE
Issue number
Cover
Issue number
Cover
Issue number
Cover
Issue number
Cover
I tried looking over and over, but I always end up on Wordpress, Joomla or Drupal sites.
And any other results is either WAY too abstract or uses numbers.
You can easily group the items visually by title by keeping track of the title and comparing it to the current one.
Something like (simple example):
// before your loop
$title = '';
// in your loop
if ($row['title'] === $title) {
// title already shown, do nothing or echo an
} else {
// new title, set and print it
$title = $row['title'];
echo htmlspecialchars($title);
}
You don't mention covers anywhere, so it's impossible to help with that until you've tried it, show the code and what the exact problem is.
There are several approaches for what you are trying to do. One approach is you also read the comic-id and then you "for-each" the result and put them in an array with the ID as Array-Index.
foreach ($rows as $row)
$arr[$row['titID']][] = $row;
If you do NOT have the need for further use of the data, you could just do
$active_id = $titID;
foreach ...
//if the current eached title-id has changed (not = $active_id)
//then set $active_id = new-title-id and start a new row
You could have an array in which each title has an array for itself.
$titles = array();
while ($row == $result->fetch_assoc()) {
if (array_key_exists($row['Title'], $titles)) {
array_push($titles[$row['Title']], $row);
}
else {
$titles[$row['Title']] = array($row);
}
}
foreach ($titles as $title=>$rows) {
echo $title . '<br>';
foreach ($rows as $row) {
echo $row['IssueNumber'] . '<br>' . $row['Cover'] . '<br>';
}
}
//Database Connection
mysql_connect("localhost", "username", "password") or die(mysql_error());
//Database selection
mysql_select_db("Database_Name") or die(mysql_error());
//your query
$data = mysql_query("SELECT * FROM comics, comicstitles WHERE comics.comTitle = comicstitles.titID ORDER BY comicstitles.titTitle ASC")
or die(mysql_error());
$temp_title="";
while($info = mysql_fetch_array( $data ))
{
$temp_title = $info['comicstitles'];
if(($temp_title == $info['comicstitles']))
{
echo "<tr>";
echo "<td>".$info[comics]."</td>";
echo "</tr>";
}
else
{
echo "<tr>"
echo "<td>".$info[comicstitles]."</td>"
echo "</tr>"
echo "<tr>";
echo "<td>".$info[comics]."</td>";
echo "</tr>";
}
}
I have been searching Google for a good amount of time and I don't know if it is the way I am asking the question or what but I can not find anything even closely relevant to this. I have a PDO SQL statement :
try
{
$query = $dbh->prepare("SELECT id, anum, first, last, signintime, counselorname,
finishtime, TIMEDIFF(finishtime, signintime) AS Total_wait FROM inoffice;");
$query->execute();
$result = $query->fetchall();
}
catch (PDOException $e) {
error_log($e->getMessage());
die($e->getMessage());
}
and then I have a table structured like so :
<table id='datatables' class='display'>
<thead>
<tr>
<th>Session Id</th>
<th>A Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>Signin Time</th>
<th>Counselor Name</th>
<th>Finish Time</th>
<th>Total Wait Time</th>
</tr>
</thead>
<tbody>
<?php
foreach($result as $row) {
?>
<tr>
<td><?=$row['id'] ?></td>
<td><?=$row['anum'] ?></td>
<td><?=$row['first'] ?></td>
<td><?=$row['last'] ?></td>
<td><?=$row['signintime'] ?></td>
<td><?=$row['counselorname'] ?></td>
<td><?=$row['finishtime'] ?></td>
<td><?= ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
My question is how on earth do I get the final part of my PDO select in that final td tag of my table? the part I am confused on is :
TIMEDIFF(finishtime, signintime) AS Total_wait
I want that to be able to be included in the last td of the table. Any help / trick / work around be awesome.
You must treat TIMEDIFF(finishtime, signintime) AS Total_wait
as its own row from the database.
resulting in this :
<td><?=$row['Total_wait'] ?></td>
now the table looks and works great :