Jump to content

Recommended Posts

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!!!

Link to comment
https://forums.phpfreaks.com/topic/64981-variable-variables/
Share on other sites

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;

}

}

Link to comment
https://forums.phpfreaks.com/topic/64981-variable-variables/#findComment-324321
Share on other sites

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!!!

Link to comment
https://forums.phpfreaks.com/topic/64981-variable-variables/#findComment-325548
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/64981-variable-variables/#findComment-325585
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.