【发布时间】:2013-12-03 11:46:39
【问题描述】:
这是我的代码。
<?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>
注意:带括号()的是金额
如果我按顺序输入货币,例如选择美元并输入金额(50),然后是欧元(2),它会保存两条记录,在字段中正确保存金额,但如果我选择 EUR (50) 和 JPY (500) 它会保存两条记录,但第一条记录将金额保存为 zero 第二条记录保存50。第一条记录必须是 50,第二条记录必须是 500。我有什么遗漏吗?
编辑:
<?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;
}
}
?>
编辑数组结果
金额: 数组([0] => [1] => 25 [2] => [3] => 50 [4] => [5] => 25 [6] => 50)
对于货币: 数组( [0] => 欧元 [1] => 日元)
【问题讨论】: