473,382 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 1499
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 name="John">POIUYTR</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 delimiter could be changed to any other. How...
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 banner </xsl:variable> <xsl:variable...
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 argument declarations are necessary." Surely that...
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 succeeds: hometabemt =...
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: <xsl:variable...
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 time-stamp from the log element. Some of the page...
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 the InsertName method to add the name parameter to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.