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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic variable assignment 1

Status
Not open for further replies.

newbiepg

Programmer
Nov 6, 2002
181
IN
I have a form that may have variable number of rows but has a fixed number of columns

I want to fetch these variables with dynamic variable assignment

e.g. first form has 2 rows with 2 columns
I have named the variables
$t1,$a1 for the first row
$t2,$a2 for the second row

if there would be a third row they would be named
$t3, $a3 and so on
I can also get the number of rows in a variable say $count

Now in the second form how can I fetch them dynamically ?



 
PHP has an undocumented feature where if you have a number of form fields named something of the form "foo[]", then in the receiving script, $_POST['foo'] will be an array.

For example, if the submitting form reads:

Code:
<html><body><form method=POST action=test_arrayinput.php>
	<input type=text name=&quot;foo[]&quot;>
	<input type=text name=&quot;bar[]&quot;><br/>
	<input type=text name=&quot;foo[]&quot;>
	<input type=text name=&quot;bar[]&quot;>
	<input type=submit>
</form></body></html>

Upon submission, the in the receiving script $_POST is:

Code:
Array
(
    [foo] => Array
        (
            [0] => a
            [1] => b
        )
    [bar] => Array
        (
            [0] => 1
            [1] => 2
        )
)
Want the best answers? Ask the best questions: TANSTAAFL!
 
thanks
I has forgotten to mention
I am still using the old version of php I think 4.06
Is it possible there
 
kubla:

If you are referencing $HTTP_POST_VARS (or the more modern $_POST) directly, then the setting of register_globals does not matter.

But, yes, PHP will converte those field-names to array elements with register_globals set to off.

I recommend against using variable variables wherever possible. Although it is a very useful feature of PHP, it can lead to maintainability problems. Using a foreach loop through an array is a lot more intuitive than composing a variable on the fly. Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top