RE: Making variables with an iteration? STILL

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm



On 06 December 2004 15:55, Robert Sossomon wrote:

> Here's the full code and the driving page:
> 
> http://rsossam-lap.ces.ncsu.edu/leadership/test.html
> 
> <snip>
> <?php
> for ($i=1; $i<10; $i++)
> {
>   if (isset ($_POST['choice'.$i]))
>   {
>    for ($j=1; $j<5; $j++)
>    {
>     $tempChoice = "choice" . $j;
>     $$tempChoice = $_POST['choice'.$i];
>    }
>   }
> }
> 
>    echo $choice1;
>    echo $choice2;
>    echo $choice3;
>    echo $choice4;
> > 
> </snip>

You've got too many iterators in there.

With $i=1, if $_POST['choice1'] is set: you set $choice1 to the value of
$_POST['choice1'].

With $i=2, if $_POST['choice2'] is set: you set $choice1 to the value of
$_POST['choice2'], then you set $choice2 to the value of $_POST['choice2'].

With $i=3, if $_POST['choice3'] is set: you set $choice1 to the value of
$_POST['choice3'], then you set $choice2 to the value of $_POST['choice3'],
then you set $choice3 to the value of $_POST['choice3'].

Etc.

Within your PHP script, I'd strongly recommend using an array, even if you
don't name your HTML form variables as array elements (which is actually
very easy, despite what a lot of people seem to think).  Then your code just
needs to look like this:

   for ($i=1; $i<10; $i++)
   {
     if (isset($_POST['choice'.$i]))
     {
       $choice[$i] = $_POST['choice'.$i];
     }
   }

   foreach ($choice as $one_choice)
   {
      echo $one_choice;
   }

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: m.ford@xxxxxxxxxxxxxx
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux