Re: Iterating varibales
| From: | Matt | Date: | Fri, 06 Jul 2001 00:56:51 +0000 |
| Subject: | Re: Iterating varibales | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-56424@lists.php.net to get a copy of this message | ||
Actually, if you make the names in the form resemble array variables,
this can be greatly simplified.
Something similar to the following might be what you are looking for:
<input type="text" name="item[0]">
<input type="text" name="item[1]">
when the form is passed to the next script, there will be a single
variable called $item - and it will be an array with two values.
This is one of the neater features of PHP... I would recommend generating
the input fields using something like the following:
for ($loop=0; $loop < $num_needed; $loop++) {
echo "<input type=\"text\" name=\"item[$loop]\"><br>";
}
in the result script you can then traverse the passed array $item
with a foreach loop (if you are using PHP4). Something like the following
could possibly work.
foreach($item as $key=>$value) {
echo "Index #$key = $value<br>";
}
At Thursday, 5 July 2001, you wrote:
>I have a site which uses two pages. The first page generates an HTML
>form with multiple rows which is then POSTed to the second page.
Sometimes
>on the first page I have to create multiple rows of INPUT fields.
To allow
>a dynamic number of INPUT fields (say someone wants to add seven
new numbers
>to there online address book), set up a while look and echo "<INPUT
>TYPE=text NAME=input_field".$inc.">"; where $inc is the incremented
variable
>in a loop. The result is something like this:
>
><INPUT TYPE=text NAME=input_field0>
><INPUT TYPE=text NAME=input_field1>
><INPUT TYPE=text NAME=input_field2>
>
> The difficulty comes when I try to access these variables on
the second
>page. Currently I am using a script like this:
>
> $String = 'input_field'.$inc;
> echo($$String);
>
>Sometimes my code requires multiple variables and conditions. When
this
>happens, using the above code is confusing a lengthy. Is there
a way to
>simplify this code at all?