Counting values

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
syno
Forum Newbie
Posts: 9
Joined: Wed Oct 15, 2003 11:22 am

Counting values

Post by syno »

if you have a Mysql database stored with dollar values for example
58.20
100.12
15.00
25.35

what is the best to take this information and out put a total for example

58.20
100.12
15.00
25.35
the total is 198.67

again thanks for all your help you have given me!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

SELECT SUM(*) AS sum FROM values;
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Code: Select all

SELECT SUM(col_I_want_added) FROM table WHERE something = somethngelse
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

Post by thegreatone2176 »

you could do something like

Code: Select all

$link = "SELECT * FROM payments";
$result = mysql_query($link);
while ($row = mysql_fetch_row($result)){
$price = $rowїmoney];
$total += $price;
}
echo "The total is " . $total;
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

thegreatone2176 wrote:you could do something like

Code: Select all

$link = "SELECT * FROM payments";
$result = mysql_query($link);
while ($row = mysql_fetch_row($result)){
$price = $rowїmoney];
$total += $price;
}
echo "The total is " . $total;
Why would you want to loop through your database when yo ucan get it in one shot
thegreatone2176
Forum Contributor
Posts: 102
Joined: Sun Jul 11, 2004 1:27 pm

Post by thegreatone2176 »

well depending on how else he will format it

he could make a receipt looking form by echoing out each payment in the loop and then after the loop echo the total.
syno
Forum Newbie
Posts: 9
Joined: Wed Oct 15, 2003 11:22 am

Post by syno »

Code: Select all

$sql = mysql_query("SELECT SUM(amount) as total FROM billdata");
while ($row = mysql_fetch_array($sql)) { 
echo $rowї'total']; 
}
give me a value like this 10046766.22

the values im asking it to add for me are like this
100.23
56.43
23.00

etc

any ideas? thanks
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

what is the type where you are storing those floating numbers in?
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

how many lines are you adding? maybe that's right? You are not using a where clause so it is getting all the records!
syno
Forum Newbie
Posts: 9
Joined: Wed Oct 15, 2003 11:22 am

Post by syno »

im storing the numbers in a varchar type field in mysql. the reason why is because INT would not store the zero first when i enterd 02 the database only showed 2.

im trying to add every value in a singel col
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

leading zeros can be done during output.. there is the DECIMAL type.
Post Reply