I'm working with an API that sends a request to my server with some POST values, including this one:
'NumMedia' => '1'
The value for NumMedia determines how many additional parameters will be received. For example if NumMedia = 1 I will get 2 additional parameters like this:
'MediaContentType0' => 'image/jpeg'
'MediaUrl0' => 'https://api.server.com/2010-04-01/Accounts/xxxxxxxx',
If NumMedia is greater than 1 I'll get an additional set of parameters like this:
'MediaContentType0' => 'image/jpeg'
'MediaUrl0' => 'https://api.server.com/2010-04-01/Accounts/xxxxxxxx'
'MediaContentType1' => 'image/jpeg'
'MediaUrl1' => 'https://api.server.com/2010-04-01/Accounts/zzzzzzzz'
and so on, where the first parameters end with 0 and increment by 1 for every additional returned set of parameters.
What I would like to do is, based on the number of parameters indicated by the value of the NumMedia parameter, loop through each of these and set them into variables and then write out some database records. Something along the lines of:
if (isset($_POST['NumMedia']) && $_POST['NumMedia'] !== '') {
$totalParams = $_POST['NumMedia'];
$counter = 0;
// set a variable for the first set of parameters
$mediaContentType = $_POST['NumMediaXXX'] // XXX = $counter
$mediaURL = $_POST['MediaUrlXXX'] // XXX = $counter
// save these variables to database record
// increment counter to get next set of parameters
// exit when $counter = $totalParams
$counter ++;
}
I can conceptually see what I need to do just not sure how to get the loop going and to reference a dynamic POST parameter and exit the loop when I hit the number of expected parameters?