MYSQL Output - HTML Table - php

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"

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.

Repeating Titles in headers when storing SQL data in table

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

Populating table from database

I am trying to populate a table with the query results I receive from a function. I am able to populate the table however my table header keeps repeating in each row. Is the any way I can stop this from happening?
You need to put table header outside of your while loop:
echo '<table border="1" style="width:100%">';
echo '<tr><td> Dep</td><td> Style</td> <td> Colour</td><td> Description</td> <td> Price</td></tr>';
while($row = mysql_fetch_assoc($get5star)){
echo'<tr><td> '.$row['departmentid']. '</td><td>'.$row['style']. '</td> <td> '.$row['colour']. '</td> <td> '.$row['description']. '</td> <td>'.$row['price'].'</td> </tr>';
}
echo '</table>';
because your table's head part is inside of loop
you should do like this
for example we get book names and writer's name :
<table>
<thead>
<th>book></th>
<th>writer</th>
</thead>
<tbody>
<?php
while($row = mysql_fetch_assoc($get5star)){
echo'
<tr><td> '.$row['book']. '</td><td>'.$row['writer']. '</td> </tr>
';
}
?>
</tbody>
</table>
however my table header keeps repeating in each row
That's because it's inside the loop. The general structure of a loop is:
(stuff that happens once)
while (some condition) {
(stuff that happens many times)
}
(stuff that happens once)
If the UI table header should happen only once (which it should), then it needs to go in one of those (stuff that happens once) locations. In this case, the first one:
// happens once
echo '<table border="1" style="width:100%">';
echo '<tr><td> Dep</td><td> Style</td> <td> Colour</td><td> Description</td> <td> Price</td></tr>';
while($row = mysql_fetch_assoc($get5star)){
// happens many times
echo'<tr><td> '.$row['departmentid']. '</td><td>'.$row['style']. '</td> <td> '.$row['colour']. '</td> <td> '.$row['description']. '</td> <td>'.$row['price'].'</td> </tr>';
}
// happens once
echo '</table>';
You have the table head code inside the loop:
while($row = mysql_fetch_assoc($get5star)){
echo '<tr><td> Dep</td><td> Style</td> <td> Colour</td><td> Description</td> <td> Price</td></tr>';
You should put that outside the while loop, like this:
echo '<table border="1" style="width:100%">';
echo '<tr><td> Dep</td><td> Style</td> <td> Colour</td><td> Description</td> <td> Price</td></tr>';
while($row = mysql_fetch_assoc($get5star)){
echo'<tr><td> '.$row['departmentid']. '</td><td>'.$row['style']. '</td> <td> '.$row['colour']. '</td> <td> '.$row['description']. '</td> <td>'.$row['price'].'</td> </tr>';
}
echo '</table>';
Worth noticing that you shouldnt use mysql , to access the Database. Use PDO ( i prefer them ) or mysqli .

Display multiple values from different rows with same column value

My code:
<?php
require_once('auth.php');
require_once('connection.php');
$value1 = $_POST['value1'];
$qry=mysql_query("SELECT * FROM table WHERE (`value1` LIKE '%".$value1."%')") or die(mysql_error());
if(mysql_num_rows($qry) > 0){
while ($result = mysql_fetch_array($qry)){
$value1 = $result['value1'];
$value2 = $result['value2'];
}}else{die ("can't find ".$value1."");}
?>
<br />
<table width="700" border="0">
<tr>
<td colspan="2" align="center"><strong>Info for <?php echo $value1; ?></strong></td>
</tr>
<tr>
<td width="100">Some text:</td>
<td width="590"><?php echo $value2;?></td>
</tr>
The code is working great when there is only one row with the same value of value1. If there are 2,3 or more rows with the same value for value1 the script displays only the values from the last row in my table found with the queried value. I want it to display separate tables with entries from all queried rows.
I've searched online for help, but all I could find is how to retrieve the values from my database, nothing helpful for me
you should move your html block to inside the while loop. try this
<br />
<table width="700" border="0">
<?php
if(mysql_num_rows($qry) > 0){
while ($result = mysql_fetch_array($qry)){
$value1 = $result['value1'];
$value2 = $result['value2'];
?>
<tr>
<td colspan="2" align="center"><strong>Info for <?php echo $value1; ?></strong></td>
</tr>
<tr>
<td width="100">Some text:</td>
<td width="590"><?php echo $value2;?></td>
</tr>
<?php
}}else{die ("can't find ".$value1."");}
?>
</table>
I moved the <table> code to before the while loop, moved the table rows to inside the loop and the table close tag after the loop. this way you get all rows.
If you don't want to show the table at all if there is no rows you can move the <table ..> and </table> tags to inside the if statement block.
Edit:
if(mysql_num_rows($qry) > 0){
?>
<br />
<table width="700" border="0">
<?php
while ($result = mysql_fetch_array($qry)){
$value1 = $result['value1'];
$value2 = $result['value2'];
?>
<tr>
<td colspan="2" align="center"><strong>Info for <?php echo $value1; ?></strong></td>
</tr>
<tr>
<td width="100">Some text:</td>
<td width="590"><?php echo $value2;?></td>
</tr>
<?php
}
echo '</table>';
}else{die ("can't find ".$value1."");}
?>
You're assigning values from the returned rows to $value1 and $value2. So each iteration of the loop replaces the last stored values in these variables.
This will effectively only give you the last row values found (last values assigned to the variables.)
You either need to push these values into a collection/array and re-loop through them to build your table, or put the table code in the initial loop.

PHP MySQL populating values from database

lets say i retrieve all of the values where their position belongs to top8.I populate them out in a table and instead of displaying different kinds of values , it displays 3 tables with 3 different values, how is this so? any help so that different values belonging to certain values will all be displayed out? i only need one table with 3 different values.
<?
$facebookID = "top8";
mysql_connect("localhost","root","password") or die(mysql_error());
mysql_select_db("schoutweet") or ie(mysql_error());
$data= mysql_query("SELECT schInitial FROM matchTable WHERE position='".$facebookID."'")
or die(mysql_error());
while($row = mysql_fetch_array($data))
{
?>
<center>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_bracket">
<tr>
<td class="brack_under cell_1"><a href="www.facebook.com"/>team 1.1><?= $row['schInitial']?><a/></td>
<td class="cell_2"> </td>
<td class="cell_3"> </td>
<td class="cell_4"> </td>
<td class="cell_5"> </td>
<td class="cell_6"> </td>
</tr>
<tr>
<td class="brack_under_right_up">team 1.2><?= $row['schInitial']?></</td>
<td class="brack_right"><!--1.2.1--></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td class="brack_right"><!--2.1--></td>
<td class="brack_under"><!--3.1--></td>
<td><!--here?--></td>
<td><!--there?--></td>
<td><!--everywhere?--></td>
</tr>
</table>
</center>
<?
}
?>
</body>
That's because your <table> tag is within the loop! Place the <table> tag outside the while loop.
place your table tags outside the while loop
Because your writing the table tag inside the while loop. Everything inside the loop is done each loop cycle. If you only want to have one table in the output, you'll have to open and close the table outside of the loop, like this:
$data= mysql_query("SELECT schInitial FROM matchTable WHERE position='".$facebookID."'")
or die(mysql_error());
?>
<center>
<table border="0" cellspacing="0" cellpadding="0" class="tbl_bracket">
<?
while($row = mysql_fetch_array($data))
{
?>
<tr>
<td class="brack_under cell_1"><a href="www.facebook.com"/>team 1.1><?= $row['schInitial']?><a/></td>
<td class="cell_2"> </td>
<td class="cell_3"> </td>
<td class="cell_4"> </td>
<td class="cell_5"> </td>
<td class="cell_6"> </td>
</tr>
<tr>
<td class="brack_under_right_up">team 1.2><?= $row['schInitial']?></</td>
<td class="brack_right"><!--1.2.1--></td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td class="brack_right"><!--2.1--></td>
<td class="brack_under"><!--3.1--></td>
<td><!--here?--></td>
<td><!--there?--></td>
<td><!--everywhere?--></td>
</tr>
<?
}
?>
</table>
</center>
That will, however, print three rows per loop and therefore per record (but you have references to the table contents in two of them, so I suppose that's what you want?).
Also take care about some not well-formed HTML you have there (e.g. the > character in the expression team 1.1> / team 1.2>. If you want to print the > character to the browser, encode it as HTML entity (> for this case). You also have a probably superfluous </ in the first column of the second row (</</td>).
you need to echo the HTML part as well in the while loop like
echo '<table>';

Categories