Re: [PHP4BETA] re:Totalling a variable in a while loop
| From: | David Robley | Date: | Fri, 18 Feb 2000 05:26:20 +0000 |
| Subject: | Re: [PHP4BETA] re:Totalling a variable in a while loop | ||
| References: | 1 | Groups: | php.version4 |
| Request: | Send a blank email to php-version4+get-10430@lists.php.net to get a copy of this message | ||
On 18 Feb, Joseph H Blythe wrote:
> Hey all,
>
> I was wondering if any one had an algorithm that would total up a
> variable in a while loop, basically what I want to do is while im
> grabbing rows from a mysql table I want to total them up in a sort of
> grand total. I have tried lots of things with no success, I could
> however insert the value each loop back into the mysql table and then
> total that column ( some how ), but I would rather have an easier php
> only solution.
>
> Any help is always appreciated :-)
>
> One day I might be good enough to help someone else for a change eh!
>
> Joseph Blythe
The SUM function in SQL should serve you well here, rather than doing
repetitive additions in PHP. Hm, something like
SELECT SUM(whatever) AS grand_total, field1, field2 FROM table WHERE
whatever
You need to give an alias to the SUM so you can reference it later.
Cheers
--
David Robley | WEBMASTER & Mail List Admin
RESEARCH CENTRE FOR INJURY STUDIES | http://www.nisu.flinders.edu.au/
AusEinet | http://auseinet.flinders.edu.au/
Flinders University, ADELAIDE, SOUTH AUSTRALIA