Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to access variable, inside variable, in for loop :)

Status
Not open for further replies.

dwessell

Programmer
Jan 26, 2006
114
US
Hey all.

I have some fields being delivered via POST.. One of the fields is numberTeam, which tells me how many phoneNumber's I'll have (Another field being delivered).

So I'd like to do something like this:

for($i=1;$i<=$numberTeam;$i++){
$phoneNumber'$i' = $_POST['phoneNumber$i'];
}

But I'm not sure of the syntax for how to access that $i variable.. I know it can be done, I've seen it.. But I just can't find it now :)

Any help would be great..

Thanks
David
 
variables are not expanded inside single quotes

instead of
Code:
 $phoneNumber'$i' = $_POST['phoneNumber$i'];
try
Code:
 $phoneNumber.$i = $_POST['phoneNumber'.$i];
 
pressed submit too soon
Code:
${"phoneNumber".$i} = $_POST['phoneNumber'.$i];
 
Not sure if I get your question right, but if you use an array type name in your input fields like this:

Code:
<input name="phoneNumber[0]" />
<input name="phoneNumber[1]" />
<input name="phoneNumber[2]" />
<!-- etc -->

The you can loop through them by

Code:
foreach($_REQUEST["phoneNumber"] as $phoneNo)
  echo $phoneNo."<br />\n";

Hope this makes sense?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top