please i am trying to echo the sumation of a column in the next row
, but it is displaying with the prev row.
this is the code
$body = "<html><body><table border='1'>
<tr>
<th>Shop Name</th>
<th>Product Name</th>
<th>Size</th>
<th>Color Name</th>
<th>Quantity</th>
<th>Price</th>
<th>Cost</th>
</tr>";
$totalPrice = 0;
$pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'");
while($row = mysql_fetch_assoc($pplresult)){
$body .= "<tr>
<td>" . $row['Sname'] ."</td>
<td>" . $row['Pname'] ."</td>
<td>" . $row['Psize'] ."</td>
<td>" . $row['Pcolour'] ."</td>
<td>" . $row['Pquantity'] ."</td>
<td>" . $row['Price'] ."</td>
<td>" . $row['Tprice'] ."</td>
</tr>";
$totalPrice += $row['Tprice'];
}
$body .= "<tr>
<td>" . $totalprice ."</td>
</tr>";
$body .="</table></body></html>";
You want to display the total under the last column right?
for that you will need to add some td' s in the later tr i.e.,
$body .="<tr>
<td colspan=6></td>
<td>" . $totalprice ."</td>
</tr>";
Even your code is not appropriate. Why would you iterate through that while loop twice for the same mysql query result set,
$pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'");
while($row = mysql_fetch_assoc($pplresult)){
$body .= "<tr>
<td>" . $row['Sname'] ."</td>
<td>" . $row['Pname'] ."</td>
<td>" . $row['Psize'] ."</td>
<td>" . $row['Pcolour'] ."</td>
<td>" . $row['Pquantity'] ."</td>
<td>" . $row['Price'] ."</td>
<td>" . $row['Tprice'] ."</td>
</tr>";
$totalprice += $row['TPrice'];
}
$body .="<tr>
<td colspan=6>Total :</td>
<td>" . $totalprice ."</td>
</tr>";
$body .="</table></body></html>";
First off, why are you running the same query twice? You can do the summation in the same iteration as when you're writing out the rows:
$body = "<html><head><title></title></head><body><table>";
$totalPrice = 0;
$pplresult = mysql_query("SELECT * FROM repplac WHERE Uname = '{$_SESSION['username']}'");
while($row = mysql_fetch_assoc($pplresult)){
$body .= "<tr>
<td>" . $row['Sname'] ."</td>
<td>" . $row['Pname'] ."</td>
<td>" . $row['Psize'] ."</td>
<td>" . $row['Pcolour'] ."</td>
<td>" . $row['Pquantity'] ."</td>
<td>" . $row['Price'] ."</td>
<td>" . $row['Tprice'] ."</td>
</tr>";
$totalPrice += $row['Tprice'];
}
// echo "$totalprice";
// Add a column with colspan 6 to push the totalprice column under the Tprice column.
$body .= '<tr>
<td colspan="6">
<td>' . $totalprice ."</td>
</tr>";
$body .="</table></body></html>";
Give the td a colspan of 7, so it's 100% wide:
$body .="<tr>
<td colspan="7">" . $totalprice ."</td>
</tr>";
Also you can improve efficiency by summing the prices in your query:
SELECT *, SUM(Tprice) AS total_price FROM repplac WHERE Uname = '{$_SESSION['username']}'
or
SELECT Sname, Pname, Psize, Pcolour, Pquantity, Price, Tprice, SUM(Tprice) AS total_price FROM repplac WHERE Uname = '{$_SESSION['username']}'
then in your PHP
$body .= ... $row['total_price'] ...;
Try to add colspan like <td colspan=7>" . $totalprice ."</td>
Related
in my mysql database there is 2 table named tblapps and tblapps2 . i have stored some of the data of an application in tblapps and rest of the data in tblapps2.
in backend of my application, currenly all the data of tblapps is getting displayed but suddenly i have a requirement that i want one more column to be shown here from tblapps2 with column name "country" and mysqli table column name "precountry" . and this is completely new for me, i am not getting anyway of doing this.
if any help it will be appreciated.
<div class="portlet-body flip-scroll">
<?php
$sql = "SELECT * FROM tblapps order by id desc";
$result = $connect->query($sql);
if ($result->num_rows > 0) {
echo "<table class='display' id='example'>
<thead class='flip-content'>
<tr>
<th>ID</th>
<th>App ID</th>
<th>Full Name</th>
<th>Journey Date</th>
<th>App type</th>
<th>Visa Type</th>
<th>Fee</th>
<th>Nationality</th>
<th>Country</th>
<th>Submit Date</th>
<th>payment</th>
<th>status</th>
</tr>
</thead>";
while($row = $result->fetch_assoc()) {
echo"<tr>
<td>" . $row["id"]. "</td>
<td>". $row["app_id"]. "</td>
<td>" . $row["firstname"]. " " . $row["lastname"]."</td>
<td>" . $row["journeydate"]. "</td>
<td>" . $row["appl_type"]. "</td>
<td>" . $row["visatype"]. "</td>
<td>" . $row["visa_fee"]. "</td>
<td>" . $row["nationality"]. "</td>
<td>" . $row["precountry"]. "</td>
<td>" . $row["sdate"]. "</td>
<td>" . $row["pay_status"]. "</td>
<td>" . $row["appstatus"]. "</td>
</tr>"; }
echo "</table>"; } else { echo "0 results";}$connect->close();?>
</div>
you can use SQL JOIN for matching values in both tables.
the implementation is depending on your tables structure
$table = "<p><table width=\"770px\">
<thead>
<tr>
<th><b>FECHA</b></th>
<th><b>PRODUCTO</b></th>
<th><b>CANTIDAD</b></th>
<th><b>PRECIO</b></th>
</tr>
</thead>
<tbody> " . while($res = $result->fetchArray(SQLITE3_ASSOC))
{
echo
"<tr>
<td>" . $res['fecha'] . "</td>
<td>" . $res['nombre'] . "</td>
<td>" . $res['cantidad'] . "</td>
<td>" . $res['precio_hospital'] . "<br></td>
</tr>";
} . "
</tbody>
</table>";
Hello friends, I have a php page that does not allow me to show the table correctly I do not know how to place the while, please can you help me.
works friend, but I want to save the result of this table in a php variable to print it in
$ pdf-> writeHTML ($table, true, false, true, false, '');
You can't use echo when you are already inside and echo. Try:
$output = '<p><table width="770px">
<thead>
<tr>
<th><b>FECHA</b></th>
<th><b>PRODUCTO</b></th>
<th><b>CANTIDAD</b></th>
<th><b>PRECIO</b></th>
</tr>
</thead>
<tbody>';
while($res = $result->fetchArray(SQLITE3_ASSOC)) {
$output .= '<tr><td>' . $res['fecha'] . '</td>' .
'<td>' . $res['nombre'] . '</td>' .
'<td>' . $res['cantidad'] . '</td>' .
'<td>' . $res['precio_hospital'] .
'<br></td></tr>';
}
$output .= '</tbody></table>';
Like this:
$myVar = "<p><table width=\"770px\">
<thead>
<tr>
<th><b>FECHA</b></th>
<th><b>PRODUCTO</b></th>
<th><b>CANTIDAD</b></th>
<th><b>PRECIO</b></th>
</tr>
</thead>
<tbody> ";
while($res = $result->fetchArray(SQLITE3_ASSOC)) {
$myVar .=
"<tr>
<td>" . $res['fecha'] . "</td>
<td>" . $res['nombre'] . "</td>
<td>" . $res['cantidad'] . "</td>
<td>" . $res['precio_hospital'] . "<br></td>
</tr>";
}
$myVar .= "</tbody></table></p>";
//echo $myVar; //if you want to see the result
//$pdf->writeHTML ($myVar, true, false, true, false, ''); //to pdf
You can't concatenate a control structure to a string. PHP will throw a syntax error.
Edited to use a variable
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();
?>
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
horizontal headings keep displaying for every row. I want the headings to just display for only the top row. What is wrong with my code? Thanks in advance.
$sql = $conn->prepare("SELECT * FROM timeSheet, timeSheetUsers WHERE timeSheet.userName=timeSheetUsers.userName AND timeSheet.userName=? ORDER BY startTime ASC");
if ($sql->execute(array($_SESSION['userName']))) {
while ($row = $sql->fetch()) {
echo "<table border ='1'>";
echo
"<tr>
<th>username</th>
<th>date</th>
<th>starTime</th>
<th>endTime</th>
<th>total</th>
</tr>
<tr>
<td>" . $row['userName'] . "</td>
<td>" . $row['date'] . "</td>
<td>" . $row['startTime'] . "</td>
<td>" . $row['endTime'] . "</td>
<td>" . $row['total'] . "</td>
</tr>";
echo "</table>";
}
}
## Resulting Page ##
you have given row containing headers for table(th) in loop, change it and make them outsite the while loop
like
$sql = $conn->prepare("SELECT * FROM timeSheet, timeSheetUsers WHERE timeSheet.userName=timeSheetUsers.userName AND timeSheet.userName=? ORDER BY startTime ASC");
if ($sql->execute(array($_SESSION['userName']))) {
echo "<table border ='1'>";
echo "<tr>
<th>username</th>
<th>date</th>
<th>starTime</th>
<th>endTime</th>
<th>total</th>
</tr>";
while ($row = $sql->fetch()) {
echo
" <tr>
<td>" . $row['userName'] . "</td>
<td>" . $row['date'] . "</td>
<td>" . $row['startTime'] . "</td>
<td>" . $row['endTime'] . "</td>
<td>" . $row['total'] . "</td>
</tr>";
}
echo "</table>";
}
The heading should not be part of the while loop, this should work
if ($sql->execute(array($_SESSION['userName']))) {
echo "<table border ='1'>";
echo
"<tr>
<th>username</th>
<th>date</th>
<th>starTime</th>
<th>endTime</th>
<th>total</th>
</tr>";
while ($row = $sql->fetch()) {
echo "<tr>
<td>" . $row['userName'] . "</td>
<td>" . $row['date'] . "</td>
<td>" . $row['startTime'] . "</td>
<td>" . $row['endTime'] . "</td>
<td>" . $row['total'] . "</td>
</tr>";
}
echo "</table>";
}
I am new to PHP and trying to learn it by creating a database of all my jobs (I'm a freelance designer). I have created the code below to generate a table to display all jobs where the description contains logo which works fine and generates several rows....
<?php
$logojobs = mysql_query("SELECT * FROM hlgd_projects WHERE description LIKE '%logo%'", $connection);
?>
View logo jobs
<?php
if ($report == 'logo') {
echo "<h1>Logo jobs</h1>";
echo "<table id='report'>
<tr>
<th></th>
<th>Status</th>
<th>Lead</th>
<th>Start</th>
<th colspan='2'>Codes</th>
<th>Client</th>
<th>Description</th>
<th>Fee</th>
<th>Contact</th>
<th colspan='2'>Invoice</th>
<th>Paid</th>
</tr>";
while ($logojob = mysql_fetch_array($logojobs)) {
echo "<tr>
<td class='id'>" . $logojob['id'] . "</td>
<td>" . $logojob['status_id'] . "</td>
<td>" . $logojob['lead_id'] . "</td>
<td>" . $logojob['date_start'] . "</td>
<td>" . $logojob['code_lead'] . "</td>
<td>" . $logojob['code_hlgd'] . "</td>
<td>" . $logojob['client'] . "</td>
<td>" . $logojob['description'] . "</td>
<td>£" . $logojob['fee'] . "</td>
<td>" . $logojob['contact'] . "</td>
<td>" . $logojob['invoice'] . "</td>
<td>" . $logojob['date_inv'] . "</td>
<td>" . $logojob['date_paid'] . "</td>
</tr>";
}
echo "</table>";
}
?>
However I would like to be able to generate reports for lots of different things and so would like to create a function to generate the table and pass the relevant arguments each time. I've done this as follows but it only generates the first row so I presume it's ignoring the while? Can anyone tell me where I'm going wrong or how to better put the code above into a function?
<?php
function report_bytype($title,$job_set,$job_name) {
$reporthead = "<h1>$title</h1>
<table id='report'>
<tr>
<th></th>
<th>Status</th>
<th>Lead</th>
<th>Start</th>
<th colspan='2'>Codes</th>
<th>Client</th>
<th>Description</th>
<th>Fee</th>
<th>Contact</th>
<th colspan='2'>Invoice</th>
<th>Paid</th>
</tr>";
while ($job_name = mysql_fetch_array($job_set)) {
$reportrows = "
<tr>
<td class='id'>" . $job_name['id'] . "</td>
<td>" . $job_name['status_id'] . "</td>
<td>" . $job_name['lead_id'] . "</td>
<td>" . $job_name['date_start'] . "</td>
<td>" . $job_name['code_lead'] . "</td>
<td>" . $job_name['code_hlgd'] . "</td>
<td>" . $job_name['client'] . "</td>
<td>" . $job_name['description'] . "</td>
<td>£" . $job_name['fee'] . "</td>
<td>" . $job_name['contact'] . "</td>
<td>" . $job_name['invoice'] . "</td>
<td>" . $job_name['date_inv'] . "</td>
<td>" . $job_name['date_paid'] . "</td>
</tr>";
}
$reportfoot = "</table>";
$reporttable = $reporthead . $reportrows . $reportfoot;
echo $reporttable;
return $reporttable;
}
?>
<?php
if($report == 'logo') {
report_bytype("logo",$logojobs,$logojob);
}
if($report == 'stationery') {
report_bytype("stationery",$stationeryjobs,$stationeryjob);
}
?>
Many thanks in advance,
Helen
The problem is this line:
while ($job_name = mysql_fetch_array($job_set)) {
$reportrows = "lots of html"
}
Every time this loops $reportrows is set to the html for that row only.
Use $reportrows .= "some html"; instead which will add each row to $reportrows rather than replace $reportrows with that row.
Edit: Replacing += with .=.
I can see why you have errors you are not concatenating your result
Please see http://php.net/manual/en/language.operators.string.php for more detailed explanations
Replace
$reportrows = "
With
$reportrows .= "
Full Script
function report_bytype($title, $job_set, $job_name) {
$reporthead = "<h1>$title</h1>
<table id='report'>
<tr>
<th></th>
<th>Status</th>
<th>Lead</th>
<th>Start</th>
<th colspan='2'>Codes</th>
<th>Client</th>
<th>Description</th>
<th>Fee</th>
<th>Contact</th>
<th colspan='2'>Invoice</th>
<th>Paid</th>
</tr>";
$reportrows = "";
while ( $job_name = mysql_fetch_array ( $job_set ) ) {
$reportrows .= "
<tr>
<td class='id'>" . $job_name ['id'] . "</td>
<td>" . $job_name ['status_id'] . "</td>
<td>" . $job_name ['lead_id'] . "</td>
<td>" . $job_name ['date_start'] . "</td>
<td>" . $job_name ['code_lead'] . "</td>
<td>" . $job_name ['code_hlgd'] . "</td>
<td>" . $job_name ['client'] . "</td>
<td>" . $job_name ['description'] . "</td>
<td>£" . $job_name ['fee'] . "</td>
<td>" . $job_name ['contact'] . "</td>
<td>" . $job_name ['invoice'] . "</td>
<td>" . $job_name ['date_inv'] . "</td>
<td>" . $job_name ['date_paid'] . "</td>
</tr>";
}
$reportfoot = "</table>";
$reporttable = $reporthead . $reportrows . $reportfoot;
echo $reporttable;
return $reporttable;
}
Thanks