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);

	$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

Edited by rpv_sen: n/a

5 Years
Discussion Span
Last Post by diafol

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.