如何使用PHP添加总计?

时间:2013-11-29 22:49:31

标签: php html

一直在试用我在php中的新技能来构建一个帐户风格系统,记录收入和付款类似于你在excel中所做的付款。我认为这将是一个很好的起点来测试我的剧照。

现在我已经建立了一个提交到数据库的表单,还有一个页面,允许您查看通过日期记录的付款。

坚持让它显示利润总额。这将根据收到的付款总额减去付款总额来计算。

我已将我的代码附在下面,我真的非常感谢我能得到的任何帮助。

<style>

</style>
<?php
include 'db-connect.php'; 

$result = mysqli_query($con,"SELECT * FROM payments");

echo "<table border='0' align='center' text-align='left'>
<tr>
<th>Title:</th>
<th>Date:</th>
<th>Incoming:</th>
<th>Outgoing:</th>
<th>Notes:</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['title'] . "</td>";
  echo "<td>" . $row['date'] . "</td>";
  echo "<td>" . $row['incoming'] . "</td>";
echo "<td>" . $row['outgoing'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>

添加以下提供的代码后:

<style>

</style>
<?php
include 'db-connect.php'; 

$result = mysqli_query($con,"SELECT * FROM payments");

$totalIncoming = 0;
    $totalOutgoing = 0;
    while($row = mysqli_fetch_array($result))
      {
      $totalIncoming .= $row['incoming'];
      $totalOutgoing .= $row['outgoing'];
}
echo "<table border='0' align='center' text-align='left'>
<tr>
<th>Title:</th>
<th>Date:</th>
<th>Incoming:</th>
<th>Outgoing:</th>
<th>Notes:</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['title'] . "</td>";
  echo "<td>" . $row['date'] . "</td>";
  echo "<td>" . $row['incoming'] . "</td>";
echo "<td>" . $row['outgoing'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
  echo "</tr>";

}
echo "</table>";

$profit = $totalIncoming - $totalOutgoing;
    echo "Profit :".$profit;


mysqli_close($con);
?>
好的,这是对的吗?

<style>

</style>
<?php
include 'db-connect.php'; 

$result = mysqli_query($con,"SELECT * FROM payments");

$totalIncoming = 0;
    $totalOutgoing = 0;
    while($row = mysqli_fetch_array($result))
      {
      $totalIncoming += $row['incoming'];
      $totalOutgoing += $row['outgoing'];
      echo "<tr>";
      echo "<td>" . $row['title'] . "</td>";
      echo "<td>" . $row['date'] . "</td>";
      echo "<td>" . $row['incoming'] . "</td>";
    echo "<td>" . $row['outgoing'] . "</td>";
    echo "<td>" . $row['notes'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";
    $profit = $totalIncoming - $totalOutgoing;
    echo "Profit :".$profit;

mysqli_close($con);
?>

1 个答案:

答案 0 :(得分:2)

试试这个

$totalIncoming = 0;
    $totalOutgoing = 0;
    while($row = mysqli_fetch_array($result))
      {
      $totalIncoming += $row['incoming'];
      $totalOutgoing += $row['outgoing'];
      echo "<tr>";
      echo "<td>" . $row['title'] . "</td>";
      echo "<td>" . $row['date'] . "</td>";
      echo "<td>" . $row['incoming'] . "</td>";
    echo "<td>" . $row['outgoing'] . "</td>";
    echo "<td>" . $row['notes'] . "</td>";
      echo "</tr>";
      }
    echo "</table>";
    $profit = $totalIncoming - $totalOutgoing;
    echo "Profit :".$profit;