Jump to content

[SOLVED] Sum of Array values


CincoPistolero

Recommended Posts

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

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

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.