user2827404
user2827404

Reputation: 49

How to add up totals using php?

been trying out my new skills in php to build an accounts style system, that logs incoming and out going payments similar to what you would do in excel. thought this would be a good starting point to test my stills.

now ive built a form which submits to a database, and also a page that allows you to view the payments logged via date.

stuck on getting it to display a profit total. This will be worked out from the incoming payments total minus the outgoing payments totals.

ive attached my code below, id really appreciate any help i can get on this.

<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);
?>

after adding code provided below:

<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);
?>

ok is this right?

<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);
?>

Upvotes: 0

Views: 87

Answers (1)

Alexander Kimaru
Alexander Kimaru

Reputation: 375

Try This

$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;

Upvotes: 2

Related Questions