XJTRy Posted March 9, 2009 Share Posted March 9, 2009 I have populated and array with values from a table column. There are only three values returned 25, 35, and 50. How can I echo the sum of the numbers? $query = "SELECT worth FROM onlinegm"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo $row['worth']; echo "<br />"; } Link to comment https://forums.phpfreaks.com/topic/148640-adding-math-array-values/ Share on other sites More sharing options...
Mchl Posted March 9, 2009 Share Posted March 9, 2009 array_sum Link to comment https://forums.phpfreaks.com/topic/148640-adding-math-array-values/#findComment-780537 Share on other sites More sharing options...
XJTRy Posted March 9, 2009 Author Share Posted March 9, 2009 Quote array_sum Thanks, I've been trying without success. $query = "SELECT worth FROM onlinegm"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo $row['worth']; echo "<br />"; } echo "Sum of values = ".array_sum($row['worth'])."<br>"; Link to comment https://forums.phpfreaks.com/topic/148640-adding-math-array-values/#findComment-780541 Share on other sites More sharing options...
Mchl Posted March 9, 2009 Share Posted March 9, 2009 $query = "SELECT worth FROM onlinegm"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_array($result)){ echo $row['worth']; $values[] = $row['worth']; echo "<br />"; } echo "Sum of values = ".array_sum($values)."<br>"; or.. if you don't actually need to store those values in array $query = "SELECT worth FROM onlinegm"; $result = mysql_query($query) or die(mysql_error()); $sum = 0; while($row = mysql_fetch_array($result)){ echo $row['worth']; $sum += $row['worth']; echo "<br />"; } echo "Sum of values = $sum<br>"; Link to comment https://forums.phpfreaks.com/topic/148640-adding-math-array-values/#findComment-780544 Share on other sites More sharing options...
XJTRy Posted March 9, 2009 Author Share Posted March 9, 2009 Thanks again. I prefer the first method of pushing data into array as it's consistent with data use in actionscript. I made the mistake of assuming the returned data was already in an array and available for purge. I did this with some concepts last night as well. Your help is appreciated! Link to comment https://forums.phpfreaks.com/topic/148640-adding-math-array-values/#findComment-780547 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.