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

Member Avatar

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.