Re: need help with PHP/MySQL
| From: | Brian Clark | Date: | Tue, 21 Nov 2000 03:25:44 +0000 |
| Subject: | Re: need help with PHP/MySQL | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-26389@lists.php.net to get a copy of this message | ||
Hello Gustavo,
(GS == "Gustavo Souza") gus74@terra.com.br writes:
GS> hello, im trying to calculate rows from a table (price field) and show the
GS> total
GS>
GS> like 200,00 +
GS> 100,00 +
GS> 50,00 +
GS> 10,00 =
GS>
GS> 360,00
GS>
GS> anyone can help me to do it with PHP and MySQL ?
Well, there is sum():
select sum(price_field) from table_name
But if you have commas and other crap in there it may do funky stuff,
I don't know. Try it an see; if you get the correct value, then you
can always use one of the functions I mention below. Is the column set
to type int?
Your other option is to do something along the lines of (if you need
to add all of the rows):
<?php
$total = 0;
...
while($row = @mysql_fetch_object($query, $link))
{
$total += $row->price_field;
...
}
?>
Of course you'll want to format the resulting number, but then there
is sprintf(), printf() and number_format().
GS> thanks
GS>
GS> GS
-Brian
--
The beatings will continue until morale improves.