php - 如何在 mysql 和 PHP 中将每个数据添加到表的列中?

标签 php mysql html-table addition

我有一个列名称“销售”,销售列内有每个客户的销售列表。如何添加每个客户的所有销售额,然后在表下回显总销售额?

enter image description here

> ?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/

相关文章:

html - 使用 HTML 和 CSS 滚动表格

php - 为什么我的 PHP 文件显示为纯文本?

php - 可以仅使用 PHP 创建 MySQL 表吗?

php - 从 php 中的 mysql 查询获取前一行

mysql - 如何在mysql中获取经理的员工层次结构?

javascript - 使用 Javascript 过滤和隐藏特定的表格行

php - 如何将 css 类添加到我的示例 php 代码中

php - 通过Ajax调用PHP文件中的MySQL连接单例对象

mysql - 导入 CSV 文件 - 日期的 mysql 数据类型

html - 添加文本溢出 : ellipsis; to table cell