472,353 Members | 1,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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
Apr 13 '06 #1
7 1449
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.

Apr 13 '06 #2
"Sjoerd" <sj******@gmail.com> skrev i meddelandet
news:11*********************@e56g2000cwe.googlegro ups.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

Apr 13 '06 #3
Garry Jones wrote:
"Sjoerd" <sj******@gmail.com> skrev i meddelandet
news:11*********************@e56g2000cwe.googlegro ups.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.
js*******@attglobal.net
==================
Apr 13 '06 #4
"Jerry Stuckle" <js*******@attglobal.net> skrev i meddelandet
news:1c******************************@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
Apr 13 '06 #5
Garry Jones wrote:
"Jerry Stuckle" <js*******@attglobal.net> skrev i meddelandet
news:1c******************************@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.
js*******@attglobal.net
==================
Apr 13 '06 #6
Jerry Stuckle wrote:
Garry Jones wrote:
"Jerry Stuckle" <js*******@attglobal.net> skrev i meddelandet
news:1c******************************@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.
js*******@attglobal.net
==================
Apr 13 '06 #7
On Thu, 2006-04-13 at 20:41 +0200, Garry Jones wrote:
"Jerry Stuckle" <js*******@attglobal.net> skrev i meddelandet
news:1c******************************@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

Apr 13 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Scott | last post by:
I have an XML Document in a format like: <Variable name="Bob">ABCDEFG</Variable> <Variable name="Steve">QWERTYUI</Variable> <Variable...
2
by: Bradford | last post by:
Question for the masses... Lets say I have variable with the following contents "aaaa bbbb ccccc dddd". The format is not specific and the space...
4
by: Frederik Sørensen | last post by:
I include a xslt stylesheet with variables for all the error messages in my system. <xsl:variable name="Banner_error_1"> errormessage 1 for...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this...
3
by: rls03 | last post by:
I have the following which creates a variable containing a relative path where <xsl:value-of select="."/returns a portion of the filename: ...
2
by: Kevin | last post by:
I am having difficulty updating a variable page-time-stamp in the following snippit. The variable time-stamp is initialized from the attribute...
2
by: Looch | last post by:
All, I'm trying to output but I can only get (brackets for clarity) when using the code below. How can I "break" into the query variable in...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.