Dave_Richards Posted November 8, 2006 Share Posted November 8, 2006 Hey all!was wondering if you could help me with my problem. I have the following code:Function within a class file:[code]public static function getSomething($database){ $query = "SELECT important FROM tblPurchases WHERE purchase = 3 AND buyer = 5"; $result = $database->Select($query); return $result; }[/code]PHP Web Page:[code]$function= pFunction::getSomething($_DATABASE);foreach($function as $result) {$important = $result;$count++;}[/code]This code counts the amount of records that match the SQL criteria and places the important variable into an array. However I would like to extract all values of "important" (which is an integer value) and add them together to produce a sum of all of the "important" values. Could somebody please advice me as to what I need to add to my code to acheive this.Thanks Link to comment https://forums.phpfreaks.com/topic/26617-adding-the-values-within-an-array/ Share on other sites More sharing options...
Psycho Posted November 8, 2006 Share Posted November 8, 2006 [code]foreach ($arrayVar as $value) { $sum += $value;}[/code] Link to comment https://forums.phpfreaks.com/topic/26617-adding-the-values-within-an-array/#findComment-121766 Share on other sites More sharing options...
Dave_Richards Posted November 8, 2006 Author Share Posted November 8, 2006 thanks for your help mate :) much appreciated.Will give the code a go tomorrow.Thanks again Link to comment https://forums.phpfreaks.com/topic/26617-adding-the-values-within-an-array/#findComment-121862 Share on other sites More sharing options...
roopurt18 Posted November 9, 2006 Share Posted November 9, 2006 Make sure you initialize $sum before you start the loop to zero! Link to comment https://forums.phpfreaks.com/topic/26617-adding-the-values-within-an-array/#findComment-121928 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.