Let me explain this as best I can. The user enters how many numbers he wants
to add. This form goes to another page. This page loops a form field and
it's name is "num" and after num is the number it is currently on. Here's
the code:

  <form name="add" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
    <tr>
      <td width="35%">How many numbers to add:</td>
      <td><input name="vars" type="text" id="vars" value="2" size="10"
maxlength="2"></td>
    </tr>
    <tr>
      <td colspan="2"><div align="center">
          <input name="submit" type="submit" id="submit" value="Next &gt;">
        </div></td>
    </tr>
  </form>
<?php
 }
 else
 {
  if(!is_numeric($_POST['vars']) || $_POST['vars'] <= "1")
  {
   echo "<a href=\"javascript:history.back(-1)\">Go back</a> and enter a
number or something greater then 1. DO NOT enter a letter or symbol.";
  }
  else
  {
?>
<form name="add" action="module.php?id=<?php echo $HTTP_GET_VARS['id'];
?>&show=calculate" method="post">
<input type="hidden" name="vars" value="<?php echo $_POST['vars']; ?>">
<?php
 $current = 1;
 do
 {
?>
    <tr>
      <td width="35%"><?php echo "Number ".$current.":"; ?></td>
      <td><input name="num<?php echo $current; ?>" type="text" id="vars"
value="0" size="25"></td>
    </tr>
<?php
 $current++;
 } while($current <= $_POST['vars'])
?>
    <tr>
      <td colspan="2"><div align="center">
          <input name="add" type="submit" id="add" value="Next &gt;">
        </div></td>
    </tr>
</form>

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...


----- Original Message -----
From: "Chris Wesley" <[EMAIL PROTECTED]>
To: "PHP List" <[EMAIL PROTECTED]>
Cc: "Stephen" <[EMAIL PROTECTED]>
Sent: Wednesday, December 04, 2002 7:44 PM
Subject: Re: [PHP] Looping Addition


> On Wed, 4 Dec 2002, Stephen wrote:
>
> > I already have that. $_POST['vars'] is a number and I already check that
you
> > on a page before. All I need to know is how to keep adding numbers until
> > there are no more to add...
>
> If you mean that $_POST['vars'] is an array of numbers:
>
> $total = 0;
> foreach( $_POST['vars'] as $number ){
> $total += $number;
> }
>
> If you mean that all vars in the POST data are numbers you want to add:
>
> $total = 0;
> foreach( $_POST as $number ){
> $total += $number;
> }
>
> g.luck,
>         ~Chris
>
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to