Can anyone point me in the right direction with the below
I have this HTML in a FROM (simplified for this example):
=============== Start HTML ===================
<select name="Postage[]" id="Unique-ID">
<option value="1">Postage Option 1</option>
<option value="2">Postage Option 2</option>
<option value="3">Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />
<select name="Postage[]" id="Unique-ID">
<option value="1">Postage Option 1</option>
<option value="2">Postage Option 2</option>
<option value="3">Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />
<select name="Postage[]" id="Unique-ID">
<option value="1">Postage Option 1</option>
<option value="2">Postage Option 2</option>
<option value="3">Postage Option 3</option>
</select>
<input name="PostagePrice[]" id="Price-Unique-ID" value="" />
================== End HTML =====================
I am collecting this from the form and setting it to an array ($arrPostageOptions) with the below code:
=============== Start PHP ===================
if (isset($_POST['Postage'])) {
if (is_array($_POST['Postage'])) {
$n = count($_POST['Postage']);
for ($i = 0; $i < $n; ++$i) {
// check to only collect completed entries
if( !empty($_POST['Postage'][$i]) && !empty($_POST['PostagePrice'][$i]) ){
// assign to array
$arrPostageOptions[$i] = array( "Postage" => $_POST['Postage'][$i], "PostagePrice" => $_POST['PostagePrice'][$i], );
}
}
}
}
=============== End PHP ===================
This results in the following array
============ array result ===============
Array(
[0] => Array
(
[Postage] => 1
[PostagePrice] => 12
)
[1] => Array
(
[Postage] => 2
[PostagePrice] => 24
)
[2] => Array
(
[Postage] => 3
[PostagePrice] => 48
)
)
============ end array result ===============
The answer (I think) I want is for each array is :
1 & 12
2 & 24
3 & 48
(that the array numbers match the postage numbers is just coincidence, I only want ‘Postage’ and ‘PostagePrice’)
I am trying the following:
=========== start of what I am trying ==========
foreach ($arrPostageOptions as $id) {
while ($id) {
$Postage = $id["Postage"];
$PostagePrice = $id["PostagePrice"];
// echo $Postage.' '.$PostagePrice.'<br>';
unset($id);
if (!$listing_obj->addPostageOptions($ListingID, $PostageOptionID, $PostagePrice, $db)) {
$err_text .= 'An error occurred adding Postage Option'.$PostageOptionID;
}
}
}
====== end of what I am trying ==============
Unfortunately this is not working as I expect (but is the closest I have gotten) . I have also tried multiple foreach loops, but again I don’t get what im after (unsettling the $id is stopping the loop from continuing for ever, but is also giving me PHP notices of Undefined variable $id)
I’m sure I know how to do this, but seem to have forgotten and have possible over complicated it as these arrays are still confusing the s**t out of me.
Any advice would be greatly welcomed!
while ($id)andunset($id)name="Postage[]"... maybename="Postage".