I want to generate a piece of code inside a function like this:
function get_variables_from_form(){
global $x1, $x2, $x3, ..., $x{$i};
the number of global variables varies because the form to generate those variables is dynamically generated. How can I write some kind of loop structure to output something into the code, in stead of hard-code every variable?
Please help.
}
how to generate a piece of code inside code
Moderator: General Moderators
Option 1: variable variables
Option 2: arrays
Option 2: arrays
Code: Select all
<input type="text" name="v[]" />
<input type="text" name="v[]" />
<input type="text" name="v[]" />
.....
<input type="text" name="v[]" />
<?php
print_r($_GET['v]);
?>- nickman013
- Forum Regular
- Posts: 764
- Joined: Sun Aug 14, 2005 12:02 am
- Location: Long Island, New York
jshpro2 wrote:Option 1: variable variables
Option 2: arrays
Code: Select all
<input type="text" name="v[]" /> <input type="text" name="v[]" /> <input type="text" name="v[]" /> ..... <input type="text" name="v[]" /> <?php print_r($_GET['v]); ?>
jshpro you forgot a single quote, on the $_GET
Code: Select all
<input type="text" name="v[]" />
<input type="text" name="v[]" />
<input type="text" name="v[]" />
.....
<input type="text" name="v[]" />
<?php
print_r($_GET['v']);
?>-
crackedPavement
- Forum Newbie
- Posts: 11
- Joined: Wed Mar 01, 2006 8:18 pm
- Location: portland, or
...
Couldn't you just add each variable to an array using the code that dynamically creates the variable, when the variable is created? Just use a simple array amend function to dynamically tack on the variable, then run a loop on those variables.. On each pass run the variable into a function and do whatever you want with it.
All the code is at php.net or clever googling will give you everything you need.
If you give more information you could get some more specific help..
Looking at the responses here I may not know exactly what you are trying to do.
All the code is at php.net or clever googling will give you everything you need.
If you give more information you could get some more specific help..
Looking at the responses here I may not know exactly what you are trying to do.