|
Posted by Jerry Stuckle on 04/13/06 19:17
Garry Jones wrote:
> "Sjoerd" <sjoerder@gmail.com> skrev i meddelandet
> news:1144926545.087071.20180@e56g2000cwe.googlegroups.com...
>
>
>>It is possible in PHP to have variable variables (e.g. specify the name
>>of the variable in another variable), but it is almost never what you
>>want.
>
>
> This seems to be the easiest way of doing it for me. To ease processing of
> the form I want to use the same code. On the form there is a input named
> "realname1". The following code sets the data entered into realname1 as
> var1.
>
> $x = 1;
> $y = "var".$x;
> $$y = $_POST['realname1'];
>
> So far so good, that works.
>
> But to be able to recycle this code I need to change the number 1 in
> realname. How do I use the same "$x" in the followingbrackets.
>
> $x = 1;
> $y = "var".$x;
> $$y = $_POST['realname' ($x)];
>
> I then want to create a do while lopp. Something like..
>
> DO WHILE $x <11
> $y = "var".$x;
> $$y = $_POST['realname' ($x)];
>
> $x = $x + 1;
> :Loop
>
> I am greatful if you someone can explain the syntax for this.
>
> Garry Jones
> Sweden
>
>
>
Garry,
Sjoerd is correct - you really want to use arrays. They're much easier than
your route, and the code much cleaner.
And you might as well get your feet wet with arrays now - they are very heavily
used in PHP. This is a great way of doing it.
Simply name your fields in the html something like:
<input name="personage[]" ...>
Then in your code (no error checking included):
$personage = $_POST['personage'];
// $personage now contains the entire list of names
foreach ($personage as $person) {
// $person contains the first name in the first loop iteration,
// the second name in the second iteration, etc.
doSomethingToPerson($person)
}
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|