Jump to content

variable variables?


justravis

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.