Page 1 of 1

how to generate a piece of code inside code

Posted: Wed Mar 01, 2006 7:50 pm
by natrindex
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.


}

Posted: Wed Mar 01, 2006 8:20 pm
by josh
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]);
?>

Posted: Wed Mar 01, 2006 8:23 pm
by nickman013
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']);
?>

Posted: Wed Mar 01, 2006 8:25 pm
by natrindex
THanks. I prefer use variable variables. How can I declare global variable variables n times and show them in the code?

...

Posted: Wed Mar 01, 2006 8:25 pm
by crackedPavement
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.

Posted: Wed Mar 01, 2006 8:35 pm
by natrindex
My main problems is that since all the dynamically generated variables in a form ca nnot be directly passed onto a method as parameter so I have to declare them as global variable.

I can use loop to manipulate them later.

I will try print_r(_GET['v']) approach to see if it works.

Posted: Wed Mar 01, 2006 9:21 pm
by feyd
General Discussion is never the best location to ask a code question. Moved to PHP - Code.