Here is my code.
<?php
if(isset($_POST['submit']) {
$currency = $_POST['currency'];
$amount = $_POST['amount'];
$integer = 0;
//Loop using while, check the count
while(count($currency) > $integer) {
$currencyArray = array(
'currency' => $currency[$integer],
'amount' => $amount[$integer]
);
//Insert statement constructed to array
//..... insert into
//Stop loop when reach to the limit
$integer = $integer + 1;
}
}
?>
<form action = "" method = "post">
<input type="checkbox" class="currency" name="currency[]" value="USD">USD
<input type="text" name="amount[]" value = "">
<input type="checkbox" class="currency" name="currency[]" value="EUR">EUR
<input type="text" name="amount[]" value = "">
<input type="checkbox" class="currency" name="currency[]" value="JPY">JPY
<input type="text" name="amount[]" value = "">
<input type="checkbox" class="currency" name="currency[]" value="PHP">PHP
<input type="text" name="amount[]" value = "">
<input type = "submit" name = "submit">
</form>
Note: with parenthesis () is the amount
I have a situation if I input the currency sequentially like I select USD and enter an amount (50) and next is EUR (2) it saves two records, saves the amount correctly on the field but if I select EUR (50) and JPY (500) it saves two records but the first record saves the amount into zero the second record saves 50. the first record must be 50 and second must be 500. Is there anything i missing?
EDIT:
<?php
if(isset($_POST['submit']) {
$currency = $_POST['currency'];
$amount = array();
$integer = 0;
foreach($_POST['amount'] as $amount_value) {
if($amount_value != '') {
$amount[] = $amount_value;
}
}
//Loop using while, check the count
while(count($currency) > $integer) {
$currencyArray = array(
'currency' => $currency[$integer],
'amount' => $amount[$integer]
);
//Insert statement constructed to array
//..... insert into
//Stop loop when reach to the limit
$integer = $integer + 1;
}
}
?>
EDIT FOR RESULT OF ARRAY
For the amount: Array ( [0] => [1] => 25 [2] => [3] => 50 [4] => [5] => 25 [6] => 50 )
For the currency: Array ( [0] => EUR [1] => JPY )