请我试着回应下一行中列的总和 ,但它显示的是prev行。
这是代码
$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>";
答案 0 :(得分:1)
给td
一个7的colspan,所以它是100%宽:
$body .="<tr>
<td colspan="7">" . $totalprice ."</td>
</tr>";
您还可以通过汇总查询中的价格来提高效率:
SELECT *, SUM(Tprice) AS total_price FROM repplac WHERE Uname = '{$_SESSION['username']}'
或
SELECT Sname, Pname, Psize, Pcolour, Pquantity, Price, Tprice, SUM(Tprice) AS total_price FROM repplac WHERE Uname = '{$_SESSION['username']}'
然后在你的PHP中
$body .= ... $row['total_price'] ...;
答案 1 :(得分:1)
首先,为什么两次运行相同的查询?您可以在与写出行时相同的迭代中进行求和:
$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>";
答案 2 :(得分:1)
您想要显示最后一列下的总数吗?
为此你需要在后面的tr中添加一些td,即
$body .="<tr>
<td colspan=6></td>
<td>" . $totalprice ."</td>
</tr>";
即使您的代码也不合适。为什么你会为同一个mysql查询结果集重复两次while循环,
$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>";
答案 3 :(得分:0)
尝试添加colspan
1>} <td colspan=7>" . $totalprice ."</td>