Jump to content

Get variable information from for that isn't in an array


hisairness

Recommended Posts

Hey guys,

 

I have an order form where a user can specify a variable number of line items.  So if they put in 4 my ajax function will create 4 line items in 1 form and will name the fields "qty1", "qty2", "qty3", "qty4".....

 

The problem is on the back end I don't have a good way to retrieve these variables..  I mean I don't want to manually type out up to qty100 and hope they don't use more than 100(I know more than 100 would be ridiculous)...  Anyways basically is there a way to use a while loop like this...

 

while ($theAmount > 0){

echo $qty$theAmount;

 

$theAmount--;

}

 

So I can loop through and put each one into a database one at a time?

 

Thanks in advance,

Dan

Well $_GET (and $_POST) are arrays, so you can loop through those, you also know the name will always start with a certain string... so all you'd need to is check if the string matches what you're expecting.

 

So if the form is send via the POST method then simple do:

If I'm mistaking this should the trick, (but I haven't written any php in like a year and this from the top of my head.. so no garantuees ;))

foreach( $_POST as $key => $value )
{
  if( strpos( $key, 'qty' ) === 0 ) //<-- note the tripple = is important as it can return boolean 0 (false) as well
  {
     /*Perform your magic here*/
  }
}

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.