Page 1 of 1

calculate two fields

Posted: Sat Jan 12, 2008 2:00 pm
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 ? )

Re: calculate two fields

Posted: Sat Jan 12, 2008 2:05 pm
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 ...

Re: calculate two fields

Posted: Sat Jan 12, 2008 2:07 pm
by RobertGonzalez
You should actually do this in the query, where it belongs, not in the application.

Re: calculate two fields

Posted: Sat Jan 12, 2008 2:09 pm
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>";