1,105,344 Community Members

how to sum array value

Member Avatar
rpv_sen
Posting Whiz in Training
260 posts since Mar 2011
Reputation Points: 14 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 1 [?]
 
0
 

I am facing problem on sum of dynamic array value.

For example :

tablename : totalamt

ref_no | Amount
1 | 58
2 | 12
3 | 11

i am using check box to select the ref_no.

now i am selecting 3 check box now and submit it.

Now it has to show sum of amount. But i am able to get only the First value i.e(58)

$receipt_ref_no = $_POST['ref_no'];

$limit = count($receipt_ref_no);

for($i=0;$i<$limit;$i++)
{
	
	$receipt_ref_no[$i] = mysql_real_escape_string($receipt_ref_no[$i]);
	
	$receipt_ref_nu = $receipt_ref_no[$i];	

echo $q = "select * from totalamt where ref_no='".$receipt_ref_nu."' group by ref_no";
$r = mysql_query($q,$conn);
$m = mysql_fetch_array($r);

But i need the below value.

Please any one can help me

Total Amount : 81

Member Avatar
diafol
Where are my eyes?
12,970 posts since Oct 2006
Reputation Points: 1,821 [?]
Q&As Helped to Solve: 1,847 [?]
Skill Endorsements: 92 [?]
Moderator
Featured
Sponsor
 
0
 

Use the IN clause

Put the checkbox values into an array - validate them and then:

SELECT SUM(fieldname) AS total FROM tablename WHERE ref_no IN ($array);

If you need to validate many values in an array, have a look at functions such as array_map() - they'll save you writing loops.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: