1,105,295 Community Members

echo field with sum of different fields

Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi

I have to echo out some fields but "field 1" "field 2" "field 3" has a numeric value.

I need to divide field 1 by field 2 and then take that result off of field 3 and echo that result.

The code is part of a table this is the code for that row

Hope someone can help

 echo '<td>' . $row['field3'] . '</td>';
Member Avatar
last1devil
Newbie Poster
8 posts since Aug 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

Maybe this helps:

$result = $row['field1'] / $row['field2'];
echo "<td>".$result."</td>";

Why do you need field3 ?

Member Avatar
adam.adamski.96155
Junior Poster
189 posts since Oct 2012
Reputation Points: 43 [?]
Q&As Helped to Solve: 40 [?]
Skill Endorsements: 6 [?]
 
0
 

This is simple maths, use parentheses to force calculations in correct order:
field3 minus (field1 divided by field2)

$result = ( $row['field3'] - ($row['field1'] / $row['field2']) );
echo "<td>".$result."</td>";
Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

HI Thanks for the help but the error comes up as Warning: Division by zero

Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have now fixed that with a @ before the brackets

Now I get the error of resource id# where the result should echo.

Can you help?

Member Avatar
adam.adamski.96155
Junior Poster
189 posts since Oct 2012
Reputation Points: 43 [?]
Q&As Helped to Solve: 40 [?]
Skill Endorsements: 6 [?]
 
0
 

Probably can help, but need to see the rest of your code to understand why there is an error.

Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

All the code is very large so I will go over what I am doing

resource is in the head
$result = ( $row['field3'] - ($row['field1'] / $row['field2']) );

i am echoing a table out with all the info in with a query and while($row = mysql_fetch_array($result))

with loads of rows, and one of those rows is your code

echo '<td>'.$result.'</td>';

Hope this is ok for the info

Member Avatar
diafol
Where are my eyes?
12,943 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,845 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

You could do a calculated field within the SQL itself:

SELECT field1, field2, field3, field3 - (field1/field2) AS calc FROM table

Obviously if field2 is empty, you'll get an error as you can't divide by zero.

Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

HI thanks for your reply , where would I put that please as i already have a query for my table

Thanks

Member Avatar
adam.adamski.96155
Junior Poster
189 posts since Oct 2012
Reputation Points: 43 [?]
Q&As Helped to Solve: 40 [?]
Skill Endorsements: 6 [?]
 
0
 

Hey that's plenty info.
The line needs to be in the while loop (it will change every time), so put it under the line:

while($row = mysql_fetch_array($result)){
    $total = ( $row['field3'] - ($row['field1'] / $row['field2']) );
    //then in the place you want the total to show:
    echo "<td>".$total."</td>";

This presumes your column names really are 'field1', 'field2' and 'field3', if not adjust accordingly. Diafol's idea was good too, well worth investigating, but you would need to show your existing query for instruction on that front.

Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

brilliant that owkrs perfect. Now only problem is i need 2 decimal points not 10 please :)

Thanks

Member Avatar
mpc123
Junior Poster
160 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Its ok thanks for your help I sorted the decimal point thing

for anyone else

'.sprintf("%.2f",$total).'

Question Answered as of 1 Year Ago by adam.adamski.96155, diafol and last1devil
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article