We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,489 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

how to sum array value

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

2
Contributors
1
Reply
1 Hour
Discussion Span
1 Year Ago
Last Updated
2
Views
rpv_sen
Posting Whiz in Training
204 posts since Mar 2011
Reputation Points: 24
Solved Threads: 18
Skill Endorsements: 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.

diafol
Keep Smiling
Moderator
10,677 posts since Oct 2006
Reputation Points: 1,632
Solved Threads: 1,514
Skill Endorsements: 57

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0594 seconds using 2.69MB