473,788 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1516
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******** *************@e 56g2000cwe.goog legroups.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 followingbracke ts.

$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******** *************@e 56g2000cwe.goog legroups.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 followingbracke ts.

$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.

doSomethingToPe rson($person)
}
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 13 '06 #4
"Jerry Stuckle" <js*******@attg lobal.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*******@attg lobal.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*******@attgl obal.net
=============== ===
Apr 13 '06 #6
Jerry Stuckle wrote:
Garry Jones wrote:
"Jerry Stuckle" <js*******@attg lobal.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*******@attgl obal.net
=============== ===
Apr 13 '06 #7
On Thu, 2006-04-13 at 20:41 +0200, Garry Jones wrote:
"Jerry Stuckle" <js*******@attg lobal.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
4561
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 name="Tim">ZXCVBNM</Variable> <Function id="1"> <Parameter type="String">Bob</Parameter> <Parameter type="String">Steve</Parameter>
2
1542
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 would i count and/or select the nodes where a child nodes value is not contained in this list. Or the reverse scenario would be sufficent whereas I count the nodes whose child nodes value is contained.
4
3247
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 name="Banner_error_2"> errormessage 2 for banner </xsl:variable>
134
7916
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 means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
166
8679
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
19204
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
7013
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 = document.getElementById('hometab'); Has anyone ever seen anything like this before, or am I dreaming?
3
3963
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 name="fileName">../../data/models/<xsl:value-of select="."/>.xml</xsl:variable> I want to use this variable as an argument of the document function: <xsl:apply-templates mode="coordinates" select="document( REFERENCE
2
3013
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 elements (children of log) in my XML source have a time-stamp attribute, so I want to use the time-stamp from the page for the page-time-stamp value. I use an xsl:choose/xsl:when/xsl:otherwise combination to setup an if/else statement to select the...
2
2028
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 the variable query? public class MyClass {
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10118
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.