Reputation: 67
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>";
Upvotes: 0
Views: 71
Reputation: 198
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>";
Upvotes: 1
Reputation: 15802
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'] ...;
Upvotes: 1
Reputation: 1481
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>";
Upvotes: 1