calculate two fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drors30
Forum Newbie
Posts: 10
Joined: Sat Jan 12, 2008 1:59 pm

calculate two fields

Post by drors30 »

i have a web page that displays some data from the database.
In my SQL table there are 4 users , for each of them there are two values of the current salary and the new salary
now when i select a user using the select field the data of the salaries is being presented.
I wanted to add another field that will add the current salary to the new salary so I added a line as follows :

echo "<td>" . $row['salary_cur'] + $row['salary_new'] ."</td>";

but in fact , i'm able to see only the value of ['salary_new'] so no calculation was effected.
Please help me to change this line so i will be able to see the result.

(i forgot to tell that i;m using Ajax here so the request is being sent from the html through a java script and then to the php. Maybe I'm not able to do calculations since Ajax is involved ? )
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: calculate two fields

Post by VladSun »

I think you should use ( ) surrounding

Code: Select all

$row['salary_cur'] + $row['salary_new']
.
Also, you could calculate it at DB layer:

Code: Select all

select salary_cur, salary_new, salary_cur + salary_new as salary_total ...
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: calculate two fields

Post by RobertGonzalez »

You should actually do this in the query, where it belongs, not in the application.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: calculate two fields

Post by califdon »

Or you could do the arithmetic in the PHP, before echoing it back to the Ajax function as a separate value.

Code: Select all

$totsalary = $row['salary_cur'] + $row['salary_new'];
echo "<td>$totsalary</td>";    
Post Reply