Jump to content

Dynamically assign variables?


oni-kun

Recommended Posts

Lets say I have a form with 40 elements. How can I dynamically assign them into a variable?...

 

I think I tried something like

for ($i=0;$i>=40;$i++) {
   ($data$i) = $_POST['data_form_'.$i];
}

 

How on earth do I assign it with a loop? :confused:

Link to comment
Share on other sites

Looping through your variables is easy when you use an array.  Which is what $_POST is.  Try using a foreach loop.  Or, perhaps it would be better if you explained what you are trying to do here, because there are very few reasons why you would need (or want) to make individual variables out of array elements.

 

I tried the above method and it didn't work:

echo $_POST['len33']; //echo's fine
for ($i=22;$i>=62;$i++) {	
                    ${'donedata'.$i} = $_POST['dec'.$i];	
                    ${'lendata'.$i} = $_POST['len'.$i];
                    ${'weightdata'.$i} = $_POST['weight'.$i]; 
     }
echo "$lendata33";// Does NOT echo

 

"Undefined variable: lendata33" <--- Do I need to use $GLOBALS since it's in for?

 

Why does it not associate $lendata33 with $_POST['len33'] ?

 

Your foreach, Maybe i'll wonder into that once I get some answers here. It looks like it should work, why not!?

Link to comment
Share on other sites

Take a closer look at your for loop for starters...

for ($i=22;$i>=62;$i++)

 

Thats why the code doesnt work, its not even initiating the loop..

and as Crayon Violent  has suggested use foreach()

 

foreach ($_POST as $key=>$value) {
   $$key = $value;
}

This will turn all the form elements into a variable so $_POST['test1'] will become $test1

 

A better understanding of what you are trying to achieve also helps :P

 

Link to comment
Share on other sites

Take a closer look at your for loop for starters...

for ($i=22;$i>=62;$i++)

 

Thats why the code doesnt work, its not even initiating the loop..

and as Crayon Violent  has suggested use foreach()

 

foreach ($_POST as $key=>$value) {
   $$key = $value;
}

This will turn all the form elements into a variable so $_POST['test1'] will become $test1

 

A better understanding of what you are trying to achieve also helps :P

 

Do I do it as so?

foreach ($_POST as $key=>$value) {
   ${'lendata'.$key} = $value;
}

What I want to do is just grab a billion textform inputs from the same page (len1, len2, len..), for some stupid script of mine, I just want it to assign every box to a variable (empty or typed) so I can insert it into the DB, heh.

 

EDIT: fixed foreach pseudoexample..

Link to comment
Share on other sites

Just curious... who is going to fill out a billion text form inputs?

 

You'll think it's completely lame, For the shite of it I wanted to create an exercise log, with each textbox as a day, so I can store each progess (length of run, weight) into all those textboxes and send them into the DB so I can compare data, and whatnot.

 

Little did I know, I needed 3942034092390 variables and a forloop or else i'd be repetatively coding for days. I know it's wrong, but just need to finish this darn loop!

 

Hmm.. I need 3 foreach loops then? because I have three variable++++'s that I'm using..

Link to comment
Share on other sites

Why dont you build the query directly from $_POST instead of making them into variables?

Creating a billion variables which are already stored in post seems like an epic waste of time to me..

 

prevent SQL injection although I suppose you could just assign it to the same array :)

Link to comment
Share on other sites

Something like this is completely acceptable (in my book)

 

$sql = "INSERT INTO `somewhere`SET ";
foreach ($_POST as $key=>$value) {
$value = do_some_injection_prevention_functions($value);
$sql .= "`".$key."`='".$value."'";
}

That is assuming that the field name is the same as the POST keys..

If they arent, and they are derived FROM the field names you can do a little mods to the $key aswell..

Link to comment
Share on other sites

Why dont you build the query directly from $_POST instead of making them into variables?

Creating a billion variables which are already stored in post seems like an epic waste of time to me..

Could do, but need to check if they're false or whatnot.

 

 

Take a look at extract, you may need array_map to ensure that the postdata is properly secured for whatever your using it for.

 

I want to make love with you.

$var_array = $_POST;
extract($var_array, EXTR_PREFIX_SAME, "xx");
echo $len33;

 

Works perfectly. I can so easily loop it now.

 

Something like this is completely acceptable (in my book)

$sql = "INSERT INTO `somewhere`SET ";
foreach ($_POST as $key=>$value) {
$value = do_some_injection_prevention_functions($value);
$sql .= "`".$key."`='".$value."'";
}

That is assuming that the field name is the same as the POST keys..

If they arent, and they are derived FROM the field names you can do a little mods to the $key aswell..

 

Wow, lots of good answers. I can use this with the other one to loop it into the database, Thanks! It's 8am, OCD made me want to finish this, I can sleep in tomorrow  :-*:P

Link to comment
Share on other sites

No, you don't want to use extract, for the same reason why you don't want to dynamically create a million individually named variables.  And why is that? Well what are you going to do with them then?

 

$x1

$x2

$x3

$x4

etc...

 

You can't easily loop that or apply something to all of them etc.. that's the whole point of an array: a list of variables you can easily manipulate.

Link to comment
Share on other sites

No, you don't want to use extract, for the same reason why you don't want to dynamically create a million individually named variables.  And why is that? Well what are you going to do with them then?

 

$x1

$x2

$x3

$x4

etc...

 

You can't easily loop that or apply something to all of them etc.. that's the whole point of an array: a list of variables you can easily manipulate.

 

I'm being incredibly ignorant and making 62 textboxes, basically 3 per day, for reasons I have for logging some stuff.

 

So when I enter something like textbox 3.. It'll update that and not the the others, so I can add upon the forms as I get the info I need for them.

 

How would I insert $lenxx into record xx for sql?.. I guess i'd need to use explode.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.