Sum of values in a while-loop

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
Czar
Forum Commoner
Posts: 58
Joined: Sun Dec 29, 2002 11:17 am

Sum of values in a while-loop

Post by Czar »

Hi

Could someone help me out. How can i get a sum of certain values in a while-loop. I pull out the values from MySQL and then loop thru values and print them.

here's the loop:

Code: Select all

if(isset($_GETї'getResults'])) {
	echo "<p>Found <b>".$totalRows_getReport."</b> rows.</p>";
	if($totalRows_getReport > '0') &#123;
		while($row_getReport = mysql_fetch_assoc($getReport)) &#123;
then i print the values. Now i need to get a sum of all values in one field. Like if you had a shopping cart displayed, where all values are listed in rows and below them is the total. I need tho do this with php, not in SQL statement.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Code: Select all

// ...
$sum = 0;
while($row_getReport = mysql_fetch_assoc($getReport)) &#123;
 $sum = $sum + $row_getReport&#1111;'field_name'];
&#125;
?

Not sure I understood though.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

posting more code may help. If the loop just sums the data or other circumstances, it may be possible to have mysql do it for you. You obviously didn't post the entire loop. I'd like to see the query as well.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

As feyd mention, this IS something SQL (imho) should tend to. I'm also puzzled by the...
Czar wrote:...I need tho do this with php, not in SQL statement.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Maybe he doesn't want to use MySQL but an alternate database?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

his code references mysql functions though.. :?
Post Reply