oni-kun Posted December 22, 2009 Share Posted December 22, 2009 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? Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 22, 2009 Share Posted December 22, 2009 $i = 0; ${'hello'.$i} = 'world'; echo $hello0; // Outputs world Quote Link to comment Share on other sites More sharing options...
.josh Posted December 22, 2009 Share Posted December 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 22, 2009 Author Share Posted December 22, 2009 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!? Quote Link to comment Share on other sites More sharing options...
aeroswat Posted December 22, 2009 Share Posted December 22, 2009 Crayon has a good point. What's the reason you can't use an array with foreach? It's a lot easier than what you are trying to do. Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 22, 2009 Author Share Posted December 22, 2009 Crayon has a good point. What's the reason you can't use an array with foreach? It's a lot easier than what you are trying to do. How would I write that simple statement as a foreach? I am usually confused with those.. Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 22, 2009 Share Posted December 22, 2009 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 Quote Link to comment Share on other sites More sharing options...
aeroswat Posted December 22, 2009 Share Posted December 22, 2009 forgive me if im wrong but you could use foreach like this foreach($_POST as $var){ echo $var; } Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 22, 2009 Author Share Posted December 22, 2009 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 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.. Quote Link to comment Share on other sites More sharing options...
aeroswat Posted December 22, 2009 Share Posted December 22, 2009 Just curious... who is going to fill out a billion text form inputs? Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 22, 2009 Author Share Posted December 22, 2009 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.. Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 22, 2009 Share Posted December 22, 2009 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.. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted December 22, 2009 Share Posted December 22, 2009 Take a look at extract, you may need array_map to ensure that the postdata is properly secured for whatever your using it for. Quote Link to comment Share on other sites More sharing options...
aeroswat Posted December 22, 2009 Share Posted December 22, 2009 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 Quote Link to comment Share on other sites More sharing options...
Buddski Posted December 22, 2009 Share Posted December 22, 2009 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.. Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 22, 2009 Author Share Posted December 22, 2009 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 Quote Link to comment Share on other sites More sharing options...
.josh Posted December 22, 2009 Share Posted December 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
oni-kun Posted December 22, 2009 Author Share Posted December 22, 2009 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. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 22, 2009 Share Posted December 22, 2009 While it is true that $_POST is an array that could be exploded or iterated over, what you really need to do is use HTML arrays for your form fields - http://us.php.net/manual/en/faq.html.php#faq.html.arrays You can than iterate (using a foreach()) over the specific fields. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.