lindm Posted January 13, 2010 Share Posted January 13, 2010 I am trying to make a script that does the following: Say I have x number of fields in a form named like the following: FieldA1y FieldA1x FieldA2y FieldA2x FieldA3y FieldA3x etc etc FieldASUMy FieldASUMx The script should calculate a sum for all fields with beginning with FieldA and ending with y as one sum and beginning with FieldA and ending with x as one sum. The field FieldASUMX or y should not be included in the sum. Not much I got so far: function sum($field) { $total = 0; //code return $calc[$field] = $total; } The script would be called like sum("FieldASUMx"). This would sum all fields beginning with FieldA and ending with x, however not the FieldASUMx field. Quote Link to comment https://forums.phpfreaks.com/topic/188384-sum-unknown-number-of-post-fields/ Share on other sites More sharing options...
PFMaBiSmAd Posted January 13, 2010 Share Posted January 13, 2010 Start by using HTML arrays - http://us2.php.net/manual/en/faq.html.php#faq.html.arrays Quote Link to comment https://forums.phpfreaks.com/topic/188384-sum-unknown-number-of-post-fields/#findComment-994509 Share on other sites More sharing options...
lindm Posted January 14, 2010 Author Share Posted January 14, 2010 Went through the array functions at http://www.w3schools.com/PHP/php_ref_array.asp but can't find a good function for me. Code to explain: <?php $_POST=array( "RRintNOcy" => "1", "RRintNOpy" => "2", "RRintVLcy" => "3", "RRintVLpy" => "4", "RRintSUMcy" => "5", "RRintSUMpy" => "6" ); function sum() { return array_sum($_POST[only values of keys beginning with RRint and ending with cy AND not containing SUM); } echo sum(); ?> Should return 4. Quote Link to comment https://forums.phpfreaks.com/topic/188384-sum-unknown-number-of-post-fields/#findComment-995153 Share on other sites More sharing options...
waynew Posted January 14, 2010 Share Posted January 14, 2010 He said HTML arrays. Quote Link to comment https://forums.phpfreaks.com/topic/188384-sum-unknown-number-of-post-fields/#findComment-995160 Share on other sites More sharing options...
lindm Posted January 15, 2010 Author Share Posted January 15, 2010 Understand. Since I need to use the id tag for all my text fields (jquery) each text field still needs to be unique. This is the code I currently use. Is it slow for larger forms/is there more efficient code? $_POST=array( "RRintNOcy" => "1", "RRintNOpy" => "2", "RRintVLcy" => "3", "RRintVLpy" => "4", "RRintSUMcy" => "5", "RRintSUMpy" => "6" ); function sum($name,$yr){ $total = 0; foreach ($_POST as $k => $v) { if(substr($k,0,-4)==$name&&substr($k,-2)==$yr){ $total += $v; } } return $total; } echo sum(RRint,cy); Quote Link to comment https://forums.phpfreaks.com/topic/188384-sum-unknown-number-of-post-fields/#findComment-995669 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.