Re: Looping Addition

From: Date: Thu, 05 Dec 2002 13:12:58 +0000
Subject: Re: Looping Addition
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-127138@lists.php.net to get a copy of this message
One more question... If I then wanted to do this for the other operations (such as multiplication, division, etc), how would I do that? ----- Original Message ----- From: "John W. Holmes" <holmes072000@charter.net> To: "'Stephen'" <webmaster@melchior.us>; "'Chris Wesley'" <cwwesley@udlug.org> Cc: "'PHP List'" <php-general@lists.php.net> Sent: Wednesday, December 04, 2002 9:01 PM Subject: RE: [PHP] Looping Addition > > 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']) > > ?> > > Change the above to: > > <?php > for($x=0;$x<$_POST['vars'];$x++) > { > ?> > <tr> > <td width="35%">Number <?php echo $x; ?>:</td> > <td><input name="num[]" type="text" id="vars" > value="0" size="25"></td> > <?php } ?> > > > <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... > > And to do the addition... > > $sum = array_sum($_POST['num']); > > ---John Holmes... > > >

« previous php.general (#127138) next »