Variables (String combination ?)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
raymedia
Forum Commoner
Posts: 27
Joined: Thu Oct 09, 2003 6:36 am
Location: Melbourne, Australia

Variables (String combination ?)

Post by raymedia »

Hi guys,

I currently have a form that have few text fields in it. After I submit the form, I would like to manipulate the field data..

For example I have

Code: Select all

<form>
<input type="text" size=40 name="xxx1" value="1">
<input type="text" size=40 name="xxx2" value="2">
<input type="text" size=40 name="xxx3" value="3">
</form>
And would like to print the data

Code: Select all

for ($i = 1; $i < 4; $i++)&#123;
	// here I want to know how to call the variables.
               // I tried this, but its not working.
               // echo ("$" . "xxx". $i);
&#125;
Thank you a lot for your time
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Assuming that the form's method is POST, you could do:

Code: Select all

for ($i = 1; $i < 4; $i++){
    echo $_POST['xxx'.$i];
}
if you were wanting to send the form via the GET method you should change $_POST for $_GET.

Mac
User avatar
raymedia
Forum Commoner
Posts: 27
Joined: Thu Oct 09, 2003 6:36 am
Location: Melbourne, Australia

Post by raymedia »

Thank you for that...really aprreciate it. If you don't mid..another quick question..if its not parsed from forms, how to build/ call these variables. For example,

Code: Select all

$xxx1 = "123";
$xxx2 = "abc";
$xxx3 = "1a2";
		
for ($i = 1; $i < 4; $i++)&#123;
      echo ("$xxx".$i);
&#125;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

To do the same using variable variables:

Code: Select all

for ($i = 1; $i < 4; $i++) {
	echo ${'xxx'.$i};
}
Mac
Post Reply