[英]issue with simple currency conversion with php
我无法获取要正确转换的所选货币的价值。 我是否正确设置了阵列?
<?php
$amount_input = filter_input(INPUT_POST, 'amount_input');
$currency1 = filter_input(INPUT_POST, 'currency_type_from');
$currency2 = filter_input(INPUT_POST, 'currency_type_to');
$currencies = array();
$currencies['GBP'] = array(
'GBP' => 1,
'EUR' => 1.09710,
'USD' => 1.28917
);
$currencies['EUR'] = array(
'EUR' => 1,
'GBP' => 0.911383,
'USD' => 1.17616
);
$currencies['USB'] = array(
'USD' => 1,
'EUR' => 0.849851,
'GBP' => 0.774699
);
$currencies = 0; // when I comment his I get an undefined index at the line below
$amount_output = $amount_input*$currencies[$currency1][$currency2];
?>
表格将货币转换为选择类型
<form action="converter.php" method="post">
<select name="currency_type_from" class="form-control">
<option value="EUR">EUR</option>
<option value="GBP">GBP</option>
<option value="USD">USD</option>
</select>
<input name="amount_input" type="text"/>
<select name="currency_type_to" class="form-control">
<option value="EUR">EUR</option>
<option value="GBP">GBP</option>
<option value="USD">USD</option>
</select>
<input name="amount_output" type="text" value="<?php echo $amount_output; ?>" /> // value are not being input converted here?
<input type="submit" name="submit" value="Convert"></input>
</form>
如果$ currencies = 0,也可能会得到未定义的索引错误; 是评论
做如下
1)如果要在同一页面上输出,则不要添加第二页的动作(形式)。 留空。
2)在Form之前添加php代码,以便获得融合价值
<?php
$amount_output = "";
$amount_input = "";
if($_POST)
{
$amount_input = filter_input(INPUT_POST, 'amount_input');
$currency1 = filter_input(INPUT_POST, 'currency_type_from');
$currency2 = filter_input(INPUT_POST, 'currency_type_to');
$currencies = array();
$currencies['GBP'] = array(
'GBP' => 1,
'EUR' => 1.09710,
'USD' => 1.28917
);
$currencies['EUR'] = array(
'EUR' => 1,
'GBP' => 0.911383,
'USD' => 1.17616
);
$currencies['USB'] = array(
'USD' => 1,
'EUR' => 0.849851,
'GBP' => 0.774699
);
$amount_output = $amount_input*$currencies[$currency1][$currency2];
}
?>
<form action="" method="post">
<select name="currency_type_from" class="form-control">
<option value="EUR">EUR</option>
<option value="GBP">GBP</option>
<option value="USD">USD</option>
</select>
<input name="amount_input" type="text" value="<?php echo $amount_input; ?>"/>
<select name="currency_type_to" class="form-control">
<option value="EUR">EUR</option>
<option value="GBP">GBP</option>
<option value="USD">USD</option>
</select>
<input name="amount_output" type="text" value="<?php echo $amount_output; ?>" />
<input type="submit" name="submit" value="Convert"></input>
</form>
将其添加到数组索引内的字符串中。 确保您的阵列密钥是USD或USB:
$amount_output = $amount_input*$currencies["$currency1"]["$currency2"];
对于测试代码,您可以在此处测试此完整的php代码:
$amount_input = 4;
$currency1 = 'GBP'; //from
$currency2 = 'USD'; //to
$currencies = array();
$currencies['GBP'] = array(
'GBP' => 1,
'EUR' => 1.09710,
'USD' => 1.28917
);
$currencies['EUR'] = array(
'EUR' => 1,
'GBP' => 0.911383,
'USD' => 1.17616
);
$currencies['USD'] = array(
'USD' => 1,
'EUR' => 0.849851,
'GBP' => 0.774699
);
//$currencies = 0; // when I comment his I get an undefined index at the line below
$amount_output = $amount_input*$currencies["$currency1"]["$currency2"];
echo '<pre>';
print_r($amount_output);
选择类型的默认选项如下所示:
currency_type_from EUR
currency_type_to EUR
但是数组键'EUR'在$currencies
中不存在,因此会出现未定义的索引错误
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.