how to generate a piece of code inside code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
natrindex
Forum Newbie
Posts: 3
Joined: Wed Mar 01, 2006 7:41 pm

how to generate a piece of code inside code

Post 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.


}
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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]);
?>
User avatar
nickman013
Forum Regular
Posts: 764
Joined: Sun Aug 14, 2005 12:02 am
Location: Long Island, New York

Post 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']);
?>
natrindex
Forum Newbie
Posts: 3
Joined: Wed Mar 01, 2006 7:41 pm

Post by natrindex »

THanks. I prefer use variable variables. How can I declare global variable variables n times and show them in the code?
crackedPavement
Forum Newbie
Posts: 11
Joined: Wed Mar 01, 2006 8:18 pm
Location: portland, or

...

Post 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.
natrindex
Forum Newbie
Posts: 3
Joined: Wed Mar 01, 2006 7:41 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

General Discussion is never the best location to ask a code question. Moved to PHP - Code.
Post Reply