Page 1 of 1

Sum of values in a while-loop

Posted: Sun Mar 06, 2005 3:39 am
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.

Posted: Sun Mar 06, 2005 6:40 am
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.

Posted: Sun Mar 06, 2005 7:17 am
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.

Posted: Sun Mar 06, 2005 7:52 am
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.

Posted: Sun Mar 06, 2005 5:05 pm
by Ambush Commander
Maybe he doesn't want to use MySQL but an alternate database?

Posted: Sun Mar 06, 2005 6:30 pm
by feyd
his code references mysql functions though.. :?