justravis Posted August 15, 2007 Share Posted August 15, 2007 i am dynamically creating a list of checkboxes for each job title in a db. the checkbox array names are something like 'job2quest[]' & the 'job5quest[]'... since i dont necessarily kno which jobs will appear, i dont want to hardcode the variables when the form is submitted. is there a way to pick apart post variable names, so the script can find the checkbox arrays? And/or is there a way to loop thru them? (like if they could be named job[]quest[]) thank you!!! Quote Link to comment https://forums.phpfreaks.com/topic/64981-variable-variables/ Share on other sites More sharing options...
dbo Posted August 15, 2007 Share Posted August 15, 2007 Maybe not the best solution but one way I've done it in the past is use a syntax like row_<integer here> and then you loop through all the rows. ie. $row = "row_$i"; $test = $_POST[$row]; Quote Link to comment https://forums.phpfreaks.com/topic/64981-variable-variables/#findComment-324314 Share on other sites More sharing options...
tibberous Posted August 15, 2007 Share Posted August 15, 2007 Are the all going to start with 'job'? Why use substr on the key on _GET or _POST? Something like... foreach($_POST as $key => $postvar){ if(!strcasecmp(substr($key, 0, 3), "job")){ list($firstpart) = explode('quest', $key); $firstpart .= "quest"; $$firstpart = $postvar; } } Quote Link to comment https://forums.phpfreaks.com/topic/64981-variable-variables/#findComment-324321 Share on other sites More sharing options...
dbo Posted August 15, 2007 Share Posted August 15, 2007 Oooooh oooh interesting approach, I like it. Nice tibberous! Quote Link to comment https://forums.phpfreaks.com/topic/64981-variable-variables/#findComment-324323 Share on other sites More sharing options...
tibberous Posted August 15, 2007 Share Posted August 15, 2007 Thanks Quote Link to comment https://forums.phpfreaks.com/topic/64981-variable-variables/#findComment-324324 Share on other sites More sharing options...
justravis Posted August 15, 2007 Author Share Posted August 15, 2007 yes, i believe that is the approach i was looking for...i will try it tomorrow..thx Quote Link to comment https://forums.phpfreaks.com/topic/64981-variable-variables/#findComment-324403 Share on other sites More sharing options...
justravis Posted August 16, 2007 Author Share Posted August 16, 2007 OK...i altered your suggestion a bit, but I wanted to ask if there were advantages to using strcasecmp() (or any other differences in yr code) that i'm not aware of? foreach($_POST as $key => $postvar) { #Var name start with 'job'? if(substr($key,0,2)=='job') { #Var name end with 'quest'? if(substr($key,-5)=='quest) { #Cycle thru checkbox array. foreach($postvar as $quest) { echo "$quest<br />"; } } } } THANKS AGAIN! You really got my mind going! I WAS STUCK!!! Quote Link to comment https://forums.phpfreaks.com/topic/64981-variable-variables/#findComment-325548 Share on other sites More sharing options...
tibberous Posted August 16, 2007 Share Posted August 16, 2007 Strcasecmp is just case insensitive. Pretty sure substr($key,0,2) only returns a string with a length of 2, ie ('jo'). Neat that you use substr with a negative start, I never think of that but I'd bet I use explode more than I have to if I remembered it did that. Quote Link to comment https://forums.phpfreaks.com/topic/64981-variable-variables/#findComment-325585 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.