Profit and loss statement

Hi there,

I have a code that subtracts the sum one table from the sum of another table in php

i have echo the amount. i have been trying the if else statement to determine whether the figure is a positive number or a negative number. If the number is positive i want it to echo profit but if the number is negative i want it to echo loss

for example if the total was £50.00 it will echo Profit : £50 but if the total was £-50.00 it would echo Loss -£50

my sql query at moment

$sql = "SELECT ((SELECT SUM(iamount) FROM income) - (SELECT SUM(eamount) FROM expenses)) AS total";

my echo

echo "Total Profit (Loss is Minus) : &nbsp &pound " .$row["total"].  "<br>";

i have tried this but does not work

if(total >= 0.00){
    echo "Total Profit : &nbsp &pound " .$row["total"].";
} else{
    echo "Total Loss : &nbsp &pound " .$row["total"].";
}

any help please ?

Please use bbcode [code]...[/code] tags when posting code, so that the forum software won’t alter what you are posting.

What is ‘total’ in that line of code? That’s the syntax for a defined CONSTANT, not a php variable. You would also be getting a php error from that line. Do you have php’s error_reporting set to E_ALL and display_errors set to ON, in the php.ini on your system, so that php would help you by reporting and displaying all the errors that it detects?

The total is from the sql query the total of the sum of income from expenses

I have a table called income with a row called iamount and a table called expenses with a row called eamount the query subtracts one from the other as total

$sql = "SELECT ((SELECT SUM(iamount) FROM income) - (SELECT SUM(eamount) FROM expenses)) AS total";

i echo the total like this

echo "Total Profit (Loss is Minus) : &nbsp &pound " .$row["total"].  "<br>";

At the moment works ok so for example if the total is a positive number lets say £50 it will echo

Total Profit (Loss is Minus) : £50.00

if the total is a negative number lets say -£50 it will echo

Total Profit (Loss is Minus) : £-50.00

what i am trying to do is use an if else statement so that if the total is a positive number it would echo

Total Profit : £50.00

or if it was a negative number it would echo

Total Loss : £-50.00

I have tried this

if(total >= 0.00){
    echo "Total Profit : &nbsp &pound " .$row["total"].";
} else{
    echo "Total Loss : &nbsp &pound " .$row["total"].";
}

All you have done in your reply is repeat what you are trying to do. We already know all that.

Did you read my reply, translating to your native language as needed, and actually look at the line of your code that I quoted? That line is NOT testing anything that exists in your code.

Hello.
I’m not sure what your error message is. But I suspect it may be something about the floating " at the end of your echo’s in the condition-blocks.

Next time you run into an error it may be helpful to post the message too. I know errors are only there to annoy you but they were intended to convey useful information to the programmer.

I’m having trouble seeing where your SQL ends and your PHP begins.
You need to build the query in PHP and submit it to MYSQL. Then read the result into PHP variables before you can echo them in PHP.

I’m not certain that you can combine the query of two different tables without joining them in some way.
You can build sample queries and try them in phpmyadmin or from the sql command line.

You aren’t showing the code to open a connection to the sql server and database. I will assume that your connection is called $handle

Try something like this.

// build the queries
$IncomeQuery="SELECT SUM(iamount) AS income   FROM income  ";
$ExpQuery =  "SELECT SUM(eamount) AS expenses FROM expenses ";

// Execute the queries
$res=mysqli_query($handle, $IncomeQuery);
$row=mysqli_fetch_assoc($res);
// fetch the value from the results
$income=$row['income'];

$res=mysqli_query($handle, $ExpQuery);
$row=mysqli_fetch_assoc($res);
// fetch the value from the results
$expenses=$row['expenses'];

// perform the calculation
$total=$income - $expensea;

// Apply the logic
if($total < 0) {$sign="Loss";}
  else($sign="Profit");

// Get rid of any negative symbol 
$total=abs($total);  
// Display the result
echo(" The enterprise had a $sign of $total");  

This could be simplified, somewhat. I have shown all the steps necessary to accomplish the desired result.

Without looking at your code, I can’t know. But, you may want to re-visit your chema. It would be simpler if you expenses and income could exist in the same table.

thank you for your help

Stop what you are doing and fix the database FIRST. The income and expenses should be in the SAME table.

Sponsor our Newsletter | Privacy Policy | Terms of Service