• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2382
  • Last Modified:

calculating total in Dreamweaver mysql php

I have a field "amount" and display many records (with  a repeat region)  I want to be able to add up all the total of the "amount" for all the records and put the total at the bottom.

I would be really grateful for any help here.
0
debbieau1
Asked:
debbieau1
1 Solution
 
alexhoganCommented:
You can do this in your SQL query;

SELECT sum(amount) as Total
FROM mytable

Your php code would look something like this;

$query = "SELECT amount, sum(amount) as Total
               FROM mytable";
$result = mysql_query($result) or die($err_msg);

while($row = mysql_fetch_array($result)){
    $amount = $row['amount'];
    $total     = $row['Total'];

    print .....

}
0
 
Saqib KhanSenior DeveloperCommented:
Should be...

$query = "SELECT amount, sum(amount) as Total
               FROM mytable Group by amount";
0
 
debbieau1Author Commented:
This worked well thanks a lot.  I had worked out to use the sum, but was grouping on the wrong field.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now