looping an array

Discussions of secure PHP coding. Security in software is important, so don't be afraid to ask. And when answering: be anal. Nitpick. No security vulnerability is too small.

Moderator: General Moderators

Post Reply
parkerlj
Forum Newbie
Posts: 2
Joined: Fri Apr 10, 2009 7:16 am

looping an array

Post by parkerlj »

Hi could someone please help me!!! I am making a quote form for someone that will do the quote then and their. You can select several checkboxes, so here's the problem i have collected the boxes they ticked in an array which will be 10,20,40 etc so how do I add all these figures together and append them onto a variable to give a final quote figure. I have been pulling my hair out for days could someone please come to the resue.

Im preying here....
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: looping an array

Post by John Cartwright »

No need for a loop to calculate the sum, simply use array_sum(), i.e.,

Code: Select all

$sum = 0;
 
if (isset($_POST['your_checkboxes_name'])) {
   $sum = array_sum($_POST['your_checkboxes_name']);
}
 
echo 'Your total is: $'. $sum;
Post Reply