我有一个列名称“销售”,销售列内有每个客户的销售列表。如何添加每个客户的所有销售额,然后在表下回显总销售额?
> ?php
include('conn.php');
$sql = "SELECT * FROM sales";
$result = mysqli_query($conn,$sql);
$count = mysqli_num_rows($result);
if($count>0)
{
echo "<html><head></head><body><table border=1>
<th>CODE</th>
<th>Employee Name</th>
<th>Customer</th>
<th>Sales</th>";
while($row = mysqli_fetch_assoc($result))
{
echo"<tr>";
echo"<td>".$row['empcode']."</td>
<td>".$row['fullname']."</td>
<td>".$row['customercode']."</td>
<td>".$row['sales']."</td></tr>";
}
echo"</table>";
}
?>
最佳答案
您需要在循环外部添加一个变量,并在循环的每次迭代中递增它
$totalSales = 0;
if($count>0)
{
echo "<html><head></head><body><table border=1>
<th>CODE</th>
<th>Employee Name</th>
<th>Customer</th>
<th>Sales</th>";
while($row = mysqli_fetch_assoc($result))
{
echo"<tr>";
echo"<td>".$row['empcode']."</td>
<td>".$row['fullname']."</td>
<td>".$row['customercode']."</td>
<td>".$row['sales']."</td></tr>";
$totalSales += $row['sales'];
}
echo"</table>";
}
关于php - 如何在 mysql 和 PHP 中将每个数据添加到表的列中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52089108/