Page 1 of 1

PHP for loop help needed

Posted: Tue Feb 10, 2009 6:38 pm
by webnhance
Hello, here is what I am trying to accomplish. I have variables being passed from a form to a different page for processing. Say these are the names of the variables in the form... variable101, variable102, variable103... variable150. I am trying to process these 50 different variables by calling them in a for loop, like so...

for ( $i=101, $i<151, $i++ ) {
echo "variable$i contains $_POST['variable{$i}']";
}

would hopefully output....

variable101 contains blahblah1 variable102 contains blahblah2 variable103 contains blahblah3 ....

But it is not working, its not trying to call variable101, variable102, variable103.

The data in the form is there if you explicitly call $variable101, $variable102... I just cannot accomplish the task using the $i, can anyone help me please? Thanks.

Re: PHP for loop help needed

Posted: Tue Feb 10, 2009 10:38 pm
by susrisha

Code: Select all

 
$tmp_var='variable'.$i;
echo "variable$i contains $_POST[$tmp_var]";
 

Re: PHP for loop help needed

Posted: Wed Feb 11, 2009 5:38 am
by webnhance
susrisha wrote:

Code: Select all

 
$tmp_var='variable'.$i;
echo "variable$i contains $_POST[$tmp_var]";
 
thanks, man, i'll try that, looks like it should work

Re: PHP for loop help needed

Posted: Wed Feb 11, 2009 9:22 am
by bonzai
for ( $i=101, $i<151, $i++ ) {
$tmp_var='variable'.$i;
echo "variable$i contains $_POST[$tmp_var]";
}