hisairness Posted February 16, 2010 Share Posted February 16, 2010 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 Link to comment https://forums.phpfreaks.com/topic/192300-get-variable-information-from-for-that-isnt-in-an-array/ Share on other sites More sharing options...
Axeia Posted February 16, 2010 Share Posted February 16, 2010 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*/ } } Link to comment https://forums.phpfreaks.com/topic/192300-get-variable-information-from-for-that-isnt-in-an-array/#findComment-1013373 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.