CincoPistolero Posted June 3, 2009 Share Posted June 3, 2009 I have an array that spits out the below data. item 1 50 item 2 45 item 3 94 item 4 100 and so on. I'd like to know how to total that second column. I tried array_sum, couldn't get that to work. Below is my while loop that spits out the data. Any help on how to add that price column and store to a variable would be appreciated. while ( $rowTBKOpt = mysql_fetch_array($resultTBKOpt)){extract($rowTBKOpt); echo "$TBKname : $$TBKprice <input type='hidden' name='tomBassOptN[]' value='$TBKname'> <input type='hidden' name='tomBassOptP[]' value='$TBKprice'><br/>"; } ?> Link to comment https://forums.phpfreaks.com/topic/160737-solved-sum-of-array-values/ Share on other sites More sharing options...
Andy-H Posted June 3, 2009 Share Posted June 3, 2009 http://uk.php.net/array_sum Link to comment https://forums.phpfreaks.com/topic/160737-solved-sum-of-array-values/#findComment-848302 Share on other sites More sharing options...
CincoPistolero Posted June 3, 2009 Author Share Posted June 3, 2009 Here is the whole code to display names and values plus the code I'm trying to use to calculate sum of array plus error I'm getting. $querySOpt = "SELECT op.basePrice AS Sprice, op.optionID, o.name AS Sname FROM optPrices op, ksOpt kso, options o WHERE kso.artistID='$artistID' AND kso.optPricesID = op.optPricesID AND op.optionID=o.optionID AND op.drumTypeID=3"; $resultSOpt = mysql_query($querySOpt) or die ("Error in query: $querySOpt. " . mysql_error()); while ( $rowSOpt = mysql_fetch_array($resultSOpt)){extract($rowSOpt); echo "$Sname : $$Sprice<input type='hidden' name='sOptN[]' value='$Sname'> <input type='hidden' name='sOptP[]' value='$Sprice'><br/>"; ----------------------- } $suTotal= array_sum($rowSOpt['sOptP']); SNARE UPGRADES TOTAL: $<?php echo $suTotal; ?> Error is below Warning: array_sum() [function.array-sum]: The argument should be an array in /home/shine2/public_html/rhino/wcc/newSubmissions/myWO.php on line 477 Link to comment https://forums.phpfreaks.com/topic/160737-solved-sum-of-array-values/#findComment-848315 Share on other sites More sharing options...
Ken2k7 Posted June 3, 2009 Share Posted June 3, 2009 Read either the manual on php.net or the error message. The argument you passed it isn't an array. Try $suTotal = array_sum($rowSOpt); Link to comment https://forums.phpfreaks.com/topic/160737-solved-sum-of-array-values/#findComment-848319 Share on other sites More sharing options...
CincoPistolero Posted June 3, 2009 Author Share Posted June 3, 2009 this is a multidimensional array. It returns 1 item1 50 2 item3 98 3 item 4 0808 etc I only want to total column 3 Link to comment https://forums.phpfreaks.com/topic/160737-solved-sum-of-array-values/#findComment-848323 Share on other sites More sharing options...
CincoPistolero Posted June 3, 2009 Author Share Posted June 3, 2009 I solved it like this. $suTotal=0; while ( $rowSOpt = mysql_fetch_array($resultSOpt)){extract($rowSOpt); echo "$Sname : $$Sprice<input type='hidden' name='sOptN[]' value='$Sname'> <input type='hidden' name='sOptP[]' value='$Sprice'><br/>"; $suTotal=$suTotal+$Sprice; } Thanks for everyone's input Link to comment https://forums.phpfreaks.com/topic/160737-solved-sum-of-array-values/#findComment-848331 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.