How can I subtract the TOTAL EXPENSES from TOTAL AMOUNT RECEIVED and display it on the TOTAL REVENUES?
The data comes from different tables from my database.. unfortunately I can't upload a pic for a clearer view..
TOTAL AMOUNT RECEIVED || 15610
TOTAL EXPENSES || 11300
TOTAL REVENUES || (this must be equal to TOTAL AMOUNT RECEIVED - TOTAL EXPENSES)
Here's my code:
<table width="383" border="1" bordercolor="#00CCFF">
<tr>
<td width="245" bgcolor="#0099FF">TOTAL AMOUNT RECIEVED</td>
<td width="128" bgcolor="#FFFFFF">
<?php
include("confstudents.php");
$id = $_GET['id'];
$query = "SELECT id, SUM(1stPayment + 2ndPayment + 3rdPayment + 4thPayment) um_payment FROM student_payments";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "" . $row['sum_payment'];
echo "<br/>";
}
?>
</td>
</tr>
<tr>
<td bgcolor="#0099FF">TOTAL EXPENSES</td>
<td bgcolor="#FFFFFF">
<?php
include("confexpenses.php");
$id = $_GET['id'];
$query = 'SELECT SUM(piece * price) tprice FROM expenses';
$result = mysql_query($query) or die(mysql_error());
while($res = mysql_fetch_assoc($result)){
echo " " . $res['tprice']; " ";
}
?>
</td>
</tr>
<tr>
<td bgcolor="#0099FF">TOTAL REVENUES</td>
<td bgcolor="#FFFFFF">
<?php
include("totalrev.php");
?>
</td>
</tr>
</table>
I'm going to work out the general answer independent of the structure/style, but essentially you want to store the return values from the frist two queries and then do some math. Let's make it simple first (KISS - Keep It Simple, Stupid), and abstract the logic from the styles.
<?php
$query = "
SELECT
id,
SUM(1stPayment + 2ndPayment + 3rdPayment + 4thPayment) AS sum_payment
FROM
student_payments";
$result = mysql_query($query) || die(mysql_error());
// Create a variable to store the sum of payments
$sum_payment = 0;
// Print out result
while($row = mysql_fetch_array($result))
{
echo "" . $row['sum_payment'];
echo "<br/>";
$sum_payment += (int)$row['sum_payment'];
}
$query = 'SELECT SUM(piece * price) tprice FROM expenses';
$result = mysql_query($query) or die(mysql_error());
// Variable to store the expenses
$expenses = 0;
while($res = mysql_fetch_assoc($result))
{
echo " " . $res['tprice']; " ";
$expenses += $res['tprice'];
}
// Calculate the difference
$total_rev = $sum_payments - $expenses;
echo '<br/>', $total_rev, '<br/>';
?>
A note on your use of $_GET['id']
- if you plan to bring in something that will eventually go into a SQL query, you should escape it: with them mysql_ library, use mysql_real_escape_string http://php.net/manual/en/function.mysql-real-escape-string.php but, in general, you should switch to using the MySQLi library - http://www.php.net/manual/en/book.mysqli.php - as it is more secure and eventually the standard mysql functions will not be supported in PHP.
If there is relation between two tables, for example id:
SELECT
SUM(r.stPayment) as RECIEVED, sum(e.piece * e.price) as EXPENSES
FROM
student_payments as r,
expenses as e
WHERE r.id = e.id and r.id = '$id'