I'm in a class, and I've been asked to retrieve information from an SQL table and display it as an invoice. I've got the basics down, however I've now been asked to format the PHP page to look something like this.
I'm a little confused as to how to do this or even where to begin for that matter.
PHP:
<?php
require("connect.php");
$inNo = $_POST["inNo"];
$sql = "SELECT invoice.invoice_no, invoice.date, invoice.cust_id, invoice.emp_id, invoice_line.prod_id, invoice_line.qty, product.cost_price, (product.cost_price * invoice_line.qty) FROM invoice INNER JOIN invoice_line ON invoice.invoice_no = invoice_line.invoice_no INNER JOIN product ON invoice_line.prod_id = product.id WHERE cust_id = '" . $inNo . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
//open table
echo '<table class="table table-striped" id="outTable">';
echo "<tr><th>Invoice no.</th><th>Date</th><th>Customer ID</th><th>Employee ID</th><th>Product ID</th><th>Qty</th><th>Price</th><th>Total cost</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
<tr>
<td>" . $row["invoice_no"]. "</td>
<td>" . $row["date"]. "</td><td>" . $row["cust_id"]. "</td>
<td>" . $row["emp_id"]. "</td><td>" . $row["prod_id"]. "</td>
<td>" . $row["qty"]. "</td><td>" . $row["cost_price"]. "</td>
<td>" . $row["(product.cost_price * invoice_line.qty)"]. "</td>
</tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Try this:
<?php
require("connect.php");
$inNo = $_POST["inNo"];
$sql = "SELECT invoice.invoice_no, invoice.date, invoice.cust_id, invoice.emp_id, invoice_line.prod_id, invoice_line.qty, product.cost_price, (product.cost_price * invoice_line.qty) FROM invoice INNER JOIN invoice_line ON invoice.invoice_no = invoice_line.invoice_no INNER JOIN product ON invoice_line.prod_id = product.id WHERE cust_id = '" . $inNo . "'";
$result = $conn->query($sql);
echo"<div style='width:50em'>";
if ($result->num_rows > 0) {
$count =0;
while($row = $result->fetch_assoc()) {
if ($count==0){
echo '<h2 style="text-align: center">Invoice no. " . $row["invoice_no"]. "</h2>';
echo '<table width=100%>';
echo "<tr style='text-align: left'><th>Date</th><th>Customer ID</th><th>Employee ID</th></tr>";
echo '<td>" . $row["date"]. "</td><td>" . $row["cust_id"]. "</td>
<td>" . $row["emp_id"]. "</td></table>';
echo '<table class="table table-striped" id="outTable" width=100% style="text-align: left">';
echo '<th>Product ID</th><th>Qty</th><th>Price</th></tr>';
}
echo "
<tr>
<td>" . $row["prod_id"]. "</td>
<td>" . $row["qty"]. "</td><td>" . $row["cost_price"]. "</td>
</tr>";
if ($count==$result->num_rows-1){
echo '<div style="text-align: right">Total cost: " . $row["(product.cost_price * invoice_line.qty)"]. "</div>';
}
$count++;
}
echo "</table></div>";
} else {
echo "0 results";
}
$conn->close();
?>
Related
Hey guys i have a problem, there is a page where i need to display data acording to date, but from 2 databases and from 2 tables from each databases,
1 Database name = highmob_comenzi Tables = players and vanzari
2 Database name = highmob_comenzi2 Tables = players and vanzari
this is the code what i got , i have tryed "select * from players, vanzari" but still no luck, even that wont extract data from both databases :(
<table class='table table-responsive-sm table-bordered table-striped table- sm' border="2px">
<thead>
<tr>
<th>Locatia Vanzari</th>
<th>Tip. Cert.</th>
<th>Nr.</th>
<th>Status Comanda Mobila</th>
<th>Status Comanda Tapiterii</th>
<th>Status Livrare</th>
<th>Ora Livrare</th>
<th>Detalii Comanda</th>
<th>Total</th>
<th>Avans</th>
<th>Rest</th>
<th></th>
</tr>
<?php
$servername = "localhost";
$username = "id";
$password = "pw";
$dbname = "highmob_comenzi2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = ("select *
from highmob_comenzi.players
cross join highmob_comenzi.vanzari
union all
select *
from highmob_comenzi2.players
cross join highmob_comenzi2.vanzari
WHERE statuslivrare >= CURRENT_DATE()");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
</thead>
<tbody>
<tr>
<td>HERE DATABASE NAME</td>
<td><a href='vezibilettransportcomenzi.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-warning'>Tip. Cert.</a></td>
<td>" . $row["id"] . "</td>
<td>
" . $row["statuscomanda"] . "
</td>
<td>" . $row["statuscomandatapiterii"] . "</td>
<td>" . $row["statuslivrare"] . "</td>
<td>" . $row["oralivrare"] . "</td>
<td>" . $row["detaliicomanda"] . "</td>
<td>
" . $row["totaldeplata"] . "</td>
<td>" . $row["avans"] . " <br><a style='color:red;'>" . $row["banipreluati"] . "</a></td>
<td>" . $row["restdeplata"] . "<br><a style='color:red;'>" . $row["banipreluatirest"] . "</a></td>
<td><a href='edit.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-primary' >Vezi comanda</a></td>
</tr>
";
}
} else {
echo "Nu sunt transporturi!";
}
$conn->close();
?>
</tbody>
</table>
you can access to the table in the same query without problem eg (assuming the tables have the same struct in both the database):
select *
from highmob_comenzi.players
cross join highmob_comenzi.vanzari
union all
select *
from highmob_comenzi2.players
cross join highmob_comenzi2.vanzari
this is only a sample for accessing to the 2 database in a single query. you must rethink the query with you join and condition
but based on you comment seem you need just (assuming that statuslivrare is a colum of the vanzari table)
select *
from highmob_comenzi.players
cross join highmob_comenzi.vanzari
WHERE vanzari.statuslivrare >= CURDATE()");
<?php
$servername = "localhost";
$username = "ID";
$password = "PW";
$dbname1 = "highmob_comenzi2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname1 );
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = ("select * from highmob_comenzi2.players cross join highmob_comenzi2.vanzari
");
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "
</thead>
<tbody>
<tr>
<td>HERE DATABASE NAME</td>
<td><a href='vezibilettransportcomenzi.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-warning'>Tip. Cert.</a></td>
<td>" . $row["id"] . "</td>
<td>
" . $row["statuscomanda"] . "
</td>
<td>" . $row["statuscomandatapiterii"] . "</td>
<td>" . $row["statuslivrare"] . "</td>
<td>" . $row["oralivrare"] . "</td>
<td>" . $row["detaliicomanda"] . "</td>
<td>
" . $row["totaldeplata"] . "</td>
<td>" . $row["avans"] . " <br><a style='color:red;'>" . $row["banipreluati"] . "</a></td>
<td>" . $row["restdeplata"] . "<br><a style='color:red;'>" . $row["banipreluatirest"] . "</a></td>
<td><a href='edit.php?id=" . $row["id"] . "' target='_blank' class='btn btn-sm btn-primary' >Vezi comanda</a></td>
</tr>
";
}
} else {
echo "Nu sunt transporturi!";
}
$conn->close();
?>
This is what i got and its working, it displays data from 1 database nammed highmob_comenzi2 and tables players and vanzari, but im getting alot of duplicates on display and also i need to insert a WHERE clause :
WHERE statuslivrare >= CURRENT_DATE()
any ideea?
I am a beginner programmer, and i want to fetch AVG() data in every rows created by the while loop,
this is the code,
<?php
$sql = "SELECT sites_id, sites_nama, sites_alamat, sites_kota_kabupaten, perpanjangan_pagu, sites_tanggal_start, sites_tanggal_finish, perpanjangan_invoice, AVG(perpanjangan_pagu) FROM site";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>
<tr>
<th>Site ID</th>
<th>Site Name</th>
<th>Alamat</th>
<th>Kab.Kota</th>
<th>Pagu</th>
<th>Harga Rata Rata</th>
<th>Awal Kontrak</th>
<th>Akhir Kontrak</th>
<th>Invoice</th>
</tr>";
// output data of each row
$rows = array();
while ($row = $result->fetch_assoc()) {
$rows = $row["AVG(perpanjangan_pagu)"];
echo "
<tr>
<td>" . $row["sites_id"] . "</td>
<td>" . $row["sites_nama"] . "</td>
<td>" . $row["sites_alamat"] . "</td>
<td>" . $row["sites_kota_kabupaten"] . "</td>
<td>" . $row["perpanjangan_pagu"] . "</td>
<td>" . $rows . "</td>
<td>" . $row["sites_tanggal_start"] . "</td>
<td>" . $row["sites_tanggal_finish"] . "</td>
<td>" . $row["perpanjangan_invoice"] . "</td>
</tr>";
}
echo "</table>";
}
else {
//echo "0 results";
}
$conn->close();
?>
and this is the image, you can see that it only return one row.
and this is the image when I deleted the avg function.
please help me to fetch the average data in every row in the table, not just only one table,
Thanks.
can you try this I have modified user code & added the AVG() column.
<?php
$sql = "SELECT sites_id, sites_nama, sites_alamat, sites_kota_kabupaten, perpanjangan_pagu, sites_tanggal_start, sites_tanggal_finish, perpanjangan_invoice, (SELECT AVG(perpanjangan_pagu) FROM site) AS 'AVG_pagu' FROM site";
$result = $conn->query($sql);
// AVG_pagu has the AVG value of all columns of `perpanjangan_pagu` in table `site`
if ($result->num_rows > 0) {
echo "<table>
<tr>
<th>Site ID</th>
<th>Site Name</th>
<th>Alamat</th>
<th>Kab.Kota</th>
<th>Pagu</th>
<th>Harga Rata Rata</th>
<th>Awal Kontrak</th>
<th>Akhir Kontrak</th>
<th>Invoice</th>
</tr>";
// output data of each row
// $rows = array(); // This is not actually required
while ($row = $result->fetch_assoc()) {
//$rows[] = $row["AVG_pagu"]; // This is not actually required
echo "
<tr>
<td>" . $row["sites_id"] . "</td>
<td>" . $row["sites_nama"] . "</td>
<td>" . $row["sites_alamat"] . "</td>
<td>" . $row["sites_kota_kabupaten"] . "</td>
<td>" . $row["perpanjangan_pagu"] . "</td>
<td>" . $row["AVG_pagu"] . "</td>
<td>" . $row["sites_tanggal_start"] . "</td>
<td>" . $row["sites_tanggal_finish"] . "</td>
<td>" . $row["perpanjangan_invoice"] . "</td>
</tr>";
}
echo "</table>";
}
else {
echo "No records found!";
}
$conn->close();
?>
I am new to HTML5 and PHP, I am trying to output a specific value in table data, If the database-retrieved-value is per condition.
My code:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>ID</th>
<th>Name Client</th>
<th>Last Update</th>
<th style="padding-left: 30%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, name, date FROM clients";
$result = mysql_query($query);
//status waarden uit
$status = "SELECT status FROM clients";
$status_ = mysql_query($status);
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .
if ($status_ > 60){echo "red";
} elseif ($status_ > 50){echo "yellow";
} else{echo "green";}
. "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
Error output
Parse error: syntax error, unexpected T_IF in
/test/composition/login/portal/portal.php
on line 204
What is the right way to solve this?
EDIT
my current code:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>Naam Client</th>
<th>Laatste Update</th>
<th style="margin-left: 40%; padding-left: 0%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable" style="font-size: 11pt;">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, naam, datum FROM clients";
$result = mysql_query($query);
$query2 = "SELECT status FROM clients";
$result2 = mysql_query($query2);
if (!empty ($result2)) {
while ($row2 = mysql_fetch_assoc($result2)) {
echo $row2['status'] . "<br />";
}
}
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['naam'] . "</td>
<td>" . $row['datum'] . "</td>
<td style='padding-left: 30%;'>";
if ($results2 > 60 && $results2 < 70) {
echo "red";
} elseif ($results2 > 50 && $results2 < 60) {
echo "yellow";
} else {
echo "green";
}
echo "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
Output the right data. but partly outside and partly inside the table.
You will have to remove the if statement out of the echo to get rid of the error Try this:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>ID</th>
<th>Name Client</th>
<th>Last Update</th>
<th style="padding-left: 30%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, name, date FROM clients";
$result = mysql_query($query);
//status waarden uit
$status = "SELECT status FROM clients";
$status_ = mysql_query($status);
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>";
if ($status_ > 60) {
echo "red";
} elseif ($status_ > 50) {
echo "yellow";
} else {
echo "green";
}
echo "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
You can't have an if statement (or any other statement, for that matter) in the middle of another statement like echo. If you want to concatenate different strings depending on a variable, you can use the conditional (AKA "ternary") operator.
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .
$status_ > 60 ? "red" : ($status_ > 50 ? "yellow" : "green" )
. "</td>
</tr>";
Try:
$status = "green";
if ($status > 50)
{
$status="yellow";
}
elseif($status>60)
{
$status="red";
}
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .$status. "</td>
</tr>";
You can't append to a string a conditional statement, assign to a variable first for example (like I posted)
This part isn't at the right place:
if ($status_ > 60){echo "red";
} elseif ($status_ > 50){echo "yellow";
} else{echo "green";}
should be:
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>";
if ($status_ > 60){
echo "red";
} elseif ($status_ > 50){
echo "yellow";
} else{
echo "green";
}
echo "</td></tr>";
Surely status_ would not come back with a number, but an array.
$status_ = mysql_query($status);
Without knowing what data is coming back, it is difficult to help.
mysql_query
I have a table that pulls information from the database. To keep it looking neat for the people visiting the webpage, I would like some columns to hide if no information is there.
My table is for results from motorsport events throughout the year. At the beginning of the year there will only be 1 event but by the time it comes to December there will be 20+ results added. So, instead of having lots of empty columns, I want them to be hidden until the title of that event has been added.
I tried to use !empty but this did not work as I wanted to it. It just moved the empty rows to the left leaving the headers not matching the correct results.
Here is my code:
<?php
//MySqli Select Query
$results = $mysqli->query("SELECT *, (u181 + u182 + u183 + u184 + u185 + u186 + u187 + u188 + u189 + u1810 + u1811 + u1812 + u1813 + u1814) AS total FROM results WHERE u18 = '1'");
$title = $mysqli->query("SELECT * FROM closedevents");
while ($t = $title->fetch_assoc()){
echo "<table width=\"1000\" border=\"0\" cellpadding=\"5\" cellspacing=\2\" class=\"entrywriting\" align=\"center\">
<tr align=\"center\">
<td>Overall</td>
<td>Competitor</td>
<td>" . $t["u18a"] . "</td>
<td>" . $t["u18b"] . "</td>
<td>" . $t["u18c"] . "</td>
<td>" . $t["u18d"] . "</td>
<td>" . $t["u18e"] . "</td>
<td>" . $t["u18f"] . "</td>
<td>" . $t["u18g"] . "</td>
<td>" . $t["u18h"] . "</td>
<td>" . $t["u18i"] . "</td>
<td>" . $t["u18j"] . "</td>
<td>" . $t["u18k"] . "</td>
<td>" . $t["u18l"] . "</td>
<td>" . $t["u18m"] . "</td>
<td>" . $t["u18n"] . "</td>
<td>Total</td>
</tr>";
}
//set counter
$counter = 1;
while($row = $results->fetch_assoc()) {
echo "<tr align=\"center\">";
echo "<td>" . $counter . "</td>";
echo '<td>'.$row["competitor"].'</td>';
echo '<td>'.$row["u181"].'</td>';
echo '<td>'.$row["u182"].'</td>';
echo '<td>'.$row["u183"].'</td>';
echo '<td>'.$row["u184"].'</td>';
echo '<td>'.$row["u185"].'</td>';
echo '<td>'.$row["u186"].'</td>';
echo '<td>'.$row["u187"].'</td>';
echo '<td>'.$row["u188"].'</td>';
echo '<td>'.$row["u189"].'</td>';
echo '<td>'.$row["u1810"].'</td>';
echo '<td>'.$row["u1811"].'</td>';
echo '<td>'.$row["u1812"].'</td>';
echo '<td>'.$row["u1813"].'</td>';
echo '<td>'.$row["u1814"].'</td>';
echo '<td>'.$row["total"].'</td>';
echo "</tr>";
$counter++; //increment count by 1
}
echo "</table>";
?>
So, if there was no title name in u18j, k, l, m, n then them columns would not be shown on the table.
A PHP alternative:
<?php
//MySqli Select Query
$results = $mysqli->query("SELECT *, (u181 + u182 + u183 + u184 + u185 + u186 + u187 + u188 + u189 + u1810 + u1811 + u1812 + u1813 + u1814) AS total FROM results WHERE u18 = '1'");
$title = $mysqli->query("SELECT * FROM closedevents");
$all_cols=array("u18a" => "u181","u18b"=> "u182","u18c"=> "u183","u18d"=> "u184","u18e"=> "u185","u18f"=> "u186","u18g"=> "u187",
"u18h"=> "u188","u18i"=> "u189","u18j"=> "u1810","u18k"=> "u1811","u18l"=> "u1812","u18m"=> "u1813","u18n"=> "u1814");
while ($t = $title->fetch_assoc()){
// remember empty cols
$empty_cols=array();
echo "<table width=\"1000\" border=\"0\" cellpadding=\"5\" cellspacing=\2\" class=\"entrywriting\" align=\"center\">
<tr align=\"center\">
<td>Overall</td>
<td>Competitor</td>";
foreach ($all_cols as $col => $value) {
if (!empty($t[$col])) {
echo "<td>" . $t[$col] . "</td>";
} else {
// set this column as empty for later
$empty_cols[]=$col;
}
} unset($col); unset($value);
echo "
<td>Total</td>
</tr>";
}
//set counter
$counter = 1;
while($row = $results->fetch_assoc()) {
echo "<tr align=\"center\">";
echo "<td>" . $counter . "</td>";
echo '<td>'.$row["competitor"].'</td>';
foreach ($all_cols as $col => $value) {
if (!in_array($col, $empty_cols)) {
// echo non-empty values
echo '<td>'.$row[$value].'</td>';
}
} unset($col); unset($value);
$counter++; //increment count by 1
}
echo "</table>";
?>
Well. I help you in jQuery. You must to include jquery library in your code, obviously. More information: http://jquery.com
First, you must to change your table with an identifier attribute:
echo "<table width=\"1000\" border=\"0\" cellpadding=\"5\" cellspacing=\2\" class=\"entrywriting\" align=\"center\">
<tr align=\"center\">
<td>Overall</td>
<td>Competitor</td>
<td rel='a'>" . $t["u18a"] . "</td>
<td rel='b'>" . $t["u18b"] . "</td>
<td rel='c'>" . $t["u18c"] . "</td>
<td rel='d'>" . $t["u18d"] . "</td>
<td rel='e'>" . $t["u18e"] . "</td>
<td>Total</td>
</tr>";
And down, in your while, the same:
echo '<td rel="a">'.$row["u181"].'</td>';
echo '<td rel="b">'.$row["u182"].'</td>';
echo '<td rel="c">'.$row["u183"].'</td>';
echo '<td rel="d">'.$row["u184"].'</td>';
echo '<td rel="e">'.$row["u185"].'</td>';
Then the javascript magic:
// select the first row and found all td in the first row
$('table.entrywriting tr:first-child').find('td').each(function() {
var text = $(this).text(); // get the inner text
if(!text) { // if text is empty
var position = $(this).attr('rel'); //position like a, b, c...
// iterate all tds in this position
$('table.entrywriting td[rel="'+position+'"]').each(function() {
$(this).hide(); // hide the td with the position
});
}
});
Good luck!
I think you can test varibables and reduc to one echo instructions like this :
<?php
$counter = 1;
$out ='';
while($row = $results->fetch_assoc()) {
$out .= "<tr align=\"center\">";
$out .= "<td>" . $counter . "</td>";
$out .= (isset($row["competitor"]) && !empty(trim($row["competitor"]))?'<td>'.$row["competitor"].'</td>':'';
..... etc
$out .= "</tr>";
$counter++; //increment count by 1
}
echo $out;
i am new to php and i try to solve this many times but i couldn't.
this is my php code
Can someone help me with this it should be easy for a php expert.
<?php
require_once 'connector.php';
$result = mysql_query('SELECT * FROM highscores ORDER BY score DESC');
$username = mysql_query('SELECT username FROM users WHERE id in(SELECT user_id FROM highscores)');
echo"<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
while ($name = mysql_fetch_array($username) )
{
echo "<tr>
<td>" . $name ['username'] . "</td>";
}
while( $row = mysql_fetch_array($result))
{
echo"
<td>" . $row ['score'] . "</td>
<td>" . $row ['date'] . "</td>
</tr>";
}
echo"
</table>
</body>
</html>
";
the table i want to take
mysql_* is deprecated! use mysqli_*
<?php
require_once 'connector.php';
$SQL = "SELECT u.username, h.score, h.date
FROM users AS u
JOIN highscores AS h ON (h.user_id = u.users_id)";
$result = mysql_query($SQL) or die( mysql_error() );
echo "<html>
<head>
<title>Highscores</title>
</head>
<body>";
if( mysql_num_rows($result) > 0 )
{
echo "<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>";
while ( $row = mysql_fetch_array($result) )
{
echo "<tr>";
printf("<td>%s</td>", $row['username']);
printf("<td>%s</td>", $row['score']);
printf("<td>%s</td>", $row['date']);
echo "</tr>";
}
echo "</table>
</body>
</html>";
}
mysql_free_result($result);
Can you try this,
$result = mysql_query('SELECT hs.score, hs.date, u.username FROM highscores as hs, users as u where hs.user_id=u.id ORDER BY score DESC');
echo "<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
while( $row = mysql_fetch_array($result))
{
echo"<tr>
<td>" . $row ['username'] . "</td>
<td>" . $row ['score'] . "</td>
<td>" . $row ['date'] . "</td>
</tr>";
}
Instead of putting everything in a double quote, you can use string concatenation with (.) operator. For example, Instead of writing like this
echo"<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
You can write like this:
echo "<html>" .
"<head>" .
"<title>Highscores</title>"
"</head>" .
"<body>" .
"<table border='1'>" .
"<tr>" .
"<th>user</th>" .
"<th>score</th>" .
"<th>Date</th>" .
"</tr>" ;
The following code block
echo "<tr>
<td>" . $name ['username'] . "</td>";
Should be written as
echo "<tr>" .
"<td>" . $name ['username'] . "</td>";
In this way the code looks more readable as well.
The second loop needs to be inside the first loop, and the closing </tr> shouldn't be inside the second loop.
<?
while ($name = mysql_fetch_array($username)) {
echo "<tr>";
echo "<td>" . $name["username"] . "</td>";
while ($row = mysql_fetch_array($result)) {
echo "<td>" . $row["score"] . "</td>";
echo "<td>" . $row["date"] . "</td>";
}
echo "</tr>";
}
?>