RE: [PHP] Making variables with an iteration? STILL
| From: | Ford, Mike | Date: | Mon, 06 Dec 2004 16:20:16 +0000 |
| Subject: | RE: [PHP] Making variables with an iteration? STILL | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-203606@lists.php.net to get a copy of this message | ||
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@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211