f



Variable in a variable name

I have an entry page which can be used to key in data for ten people. My 
assigned variables for each field are in line with

$personage1
$personage2
....etc
$personage9
$personage10

and so on. (I have 8 fields for every person, 80 values in all).

To simplify my code and make changes easier I would like to create and use 
these variables with a variable "x" in the name

This by recycling the code with $personX in and changing the value of x and 
running the code ten times. Probably in some kind of do while loop (fairly 
new to php, but I have been using Excel VB for years and am familiar with 
the syntax and effectiveness of Do While Loops).

1) how do I put a variable in a variable name?
2) syntax for a do while loop with this variable name.

The beauty is if I can get this working I can make x=1 the first time adding 
one each time and using value of X in my while statement.


Garry Jones
Sweden 


0
4/13/2006 10:50:03 AM
comp.lang.php 32646 articles. 0 followers. Post Follow

7 Replies
1146 Views

Similar Articles

[PageSpeed] 50

Garry Jones wrote:
> This by recycling the code with $personX in and changing the value of x and
> running the code ten times. Probably in some kind of do while loop (fairly
> new to php, but I have been using Excel VB for years and am familiar with
> the syntax and effectiveness of Do While Loops).

You probably want to use arrays, which are variables which can contain
multiple values.
$personage[1] = 'John';
$personage[2] = 'Gerry';

You can use $personage[1] as you would use any other variable. You can
loop through this array with:
foreach ($personage as $person) {
    echo $person;
}
This would print "JohnGerry".

See
http://nl2.php.net/manual/en/language.types.array.php
for more information about arrays.

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.

0
sjoerder (227)
4/13/2006 11:09:05 AM
"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



0
4/13/2006 2:53:40 PM
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
==================
0
jstucklex (14659)
4/13/2006 4:17:12 PM
"Jerry Stuckle" <jstucklex@attglobal.net> skrev i meddelandet 
news:1cydncizcunm8aPZnZ2dnUVZ_uudnZ2d@comcast.com...

> Sjoerd is correct - you really want to use arrays.  They're much easier 
> than your route, and the code much cleaner.

I have looked at arrays. My problem is that some people only enter their 
christain name and leave the surname blank. Other strange user combinations 
are possible, people can jump to a lower place in the form and use the forth 
group of input fields for the third person for instance.

As I am reading in up to 8 values for each person I saw that I was going to 
have to synchronise the input if I use arrays. Data entered into each of the 
groups of fields has to be kept together. As I am new to arrays and have 
already got the code sorted out I thought a variable name would be the 
simpliest way of doing this.

However, sticking ones head in the mud and refusing to adapt to the correct 
tool for the job never pays in the long term. So a few tips about keeping 
input data in groups so that an array reading does not group together the 
information from different groups of fields would come in very handy for me.

Garry Jones
Sweden 


0
4/13/2006 6:41:53 PM
On Thu, 2006-04-13 at 20:41 +0200, Garry Jones wrote:
> "Jerry Stuckle" <jstucklex@attglobal.net> skrev i meddelandet 
> news:1cydncizcunm8aPZnZ2dnUVZ_uudnZ2d@comcast.com...
> 
> > Sjoerd is correct - you really want to use arrays.  They're much easier 
> > than your route, and the code much cleaner.
> 
> I have looked at arrays. My problem is that some people only enter their 
> christain name and leave the surname blank. Other strange user combinations 
> are possible, people can jump to a lower place in the form and use the forth 
> group of input fields for the third person for instance.
> 
> As I am reading in up to 8 values for each person I saw that I was going to 
> have to synchronise the input if I use arrays. Data entered into each of the 
> groups of fields has to be kept together. As I am new to arrays and have 
> already got the code sorted out I thought a variable name would be the 
> simpliest way of doing this.
> 
> However, sticking ones head in the mud and refusing to adapt to the correct 
> tool for the job never pays in the long term. So a few tips about keeping 
> input data in groups so that an array reading does not group together the 
> information from different groups of fields would come in very handy for me.
> 
> Garry Jones
> Sweden 
> 
> 

Garry,

If your input fields are all text fields, and you assign them variable
names (<input type="text" name="personage[]">), then any empty fields
will simply be an empty string. However, they will still hold a place in
the array.

For example, if the 2nd person enters their age, but the 1st person
doesn't:

$_POST['personage'][0] will equal ''
$_POST['personage'][1] will equal '25' (or whatever age they put in).

The point is, if you have your text fields named personage[] and
personfname[], and have the same fields for each entry, then
$_POST['personage'][2] will correspond with $_POST['personfname'][2].

Does that help?

Scott

0
nospam7049 (23)
4/13/2006 8:02:14 PM
Garry Jones wrote:
> "Jerry Stuckle" <jstucklex@attglobal.net> skrev i meddelandet 
> news:1cydncizcunm8aPZnZ2dnUVZ_uudnZ2d@comcast.com...
> 
> 
>>Sjoerd is correct - you really want to use arrays.  They're much easier 
>>than your route, and the code much cleaner.
> 
> 
> I have looked at arrays. My problem is that some people only enter their 
> christain name and leave the surname blank. Other strange user combinations 
> are possible, people can jump to a lower place in the form and use the forth 
> group of input fields for the third person for instance.
> 
> As I am reading in up to 8 values for each person I saw that I was going to 
> have to synchronise the input if I use arrays. Data entered into each of the 
> groups of fields has to be kept together. As I am new to arrays and have 
> already got the code sorted out I thought a variable name would be the 
> simpliest way of doing this.
> 
> However, sticking ones head in the mud and refusing to adapt to the correct 
> tool for the job never pays in the long term. So a few tips about keeping 
> input data in groups so that an array reading does not group together the 
> information from different groups of fields would come in very handy for me.
> 
> Garry Jones
> Sweden 
> 
> 

Garry,

OK, in that case name you fields with the array index, i.e.

   <type=input, name="personage[1]" ...
   <type=input, name="personage[2]" ...

This works just as well.


-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
4/13/2006 8:25:21 PM
Jerry Stuckle wrote:
> Garry Jones wrote:
> 
>> "Jerry Stuckle" <jstucklex@attglobal.net> skrev i meddelandet 
>> news:1cydncizcunm8aPZnZ2dnUVZ_uudnZ2d@comcast.com...
>>
>>
>>> Sjoerd is correct - you really want to use arrays.  They're much 
>>> easier than your route, and the code much cleaner.
>>
>>
>>
>> I have looked at arrays. My problem is that some people only enter 
>> their christain name and leave the surname blank. Other strange user 
>> combinations are possible, people can jump to a lower place in the 
>> form and use the forth group of input fields for the third person for 
>> instance.
>>
>> As I am reading in up to 8 values for each person I saw that I was 
>> going to have to synchronise the input if I use arrays. Data entered 
>> into each of the groups of fields has to be kept together. As I am new 
>> to arrays and have already got the code sorted out I thought a 
>> variable name would be the simpliest way of doing this.
>>
>> However, sticking ones head in the mud and refusing to adapt to the 
>> correct tool for the job never pays in the long term. So a few tips 
>> about keeping input data in groups so that an array reading does not 
>> group together the information from different groups of fields would 
>> come in very handy for me.
>>
>> Garry Jones
>> Sweden
>>
> 
> Garry,
> 
> OK, in that case name you fields with the array index, i.e.
> 
>   <type=input, name="personage[1]" ...
>   <type=input, name="personage[2]" ...
> 
> This works just as well.
> 
> 

A minor correction to the above, and more clarification. It will work, but if 
you don't specify an index in your html, PHP starts arrays at index zero.  So to 
be consistent, I should have said:

  <type=input name="personage[0]" ...
  <type=input name="address[0]" ...
  <type=input name="personage[1]" ...
  <type=input name="address[1]" ...

for consistency.

Then you can access the data with

  for ($i=0; i <= 10; i++) {
    $name = $_POST["personage[$i]";
    $addr = $_POST["address[$i]"];

etc.

Of course, you will need to see if the variable is set - if they don't enter 
something in the field you won't get it in the $_POST variable.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
4/13/2006 8:32:43 PM
Reply: