RE: [PHP] Looping Addition
| From: | Ford, Mike [LSS] | Date: | Fri, 06 Dec 2002 19:27:02 +0000 |
| Subject: | RE: [PHP] Looping Addition | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-127303@lists.php.net to get a copy of this message | ||
----- Original Message -----
From: "Chris Wesley" <cwwesley@udlug.org>
To: "PHP List" <php-general@lists.php.net>
> On Wed, 4 Dec 2002, Stephen wrote:
> > This is only a snippet, there is more to it but for simplicities
sake...
> > Then I calculate it. My question is, how would I loop the adding? I
hope
you
> > understand this now...
>
> Ah!, I think I understand better now. You want to add num1, num2,
num3,
> ... numN, which are inputs from the second form in your sequence.
Gotcha.
>
> If you name /all/ the form elements as "nums[]" instead of
individually as
> "num${current}", the numbers put into the form will all be accessible
in
> one array to the PHP script that does the adding. Then you can just
loop
> over the array of numbers.
>
> In your second form, change this:
> "<input name="num<?php echo $current; ?>" type="text"
> id="vars"
value="0"
> size="25">"
>
> To this:
> "<input name="nums[]" type="text" id="vars"
> value="0" size="25">
>
> Then in the script that adds the numbers:
> $total = 0;
> foreach( $_POST['nums'] as $number ){
> $total += $number;
> }
Or even $total = array_sum($_POST['nums'])
Cheers!
Mike