使用php进行简单货币转换的问题

时间:2017-08-18 08:58:30

标签: 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> 

如果$ currency = 0,也可能会出现未定义的索引错误;是评论

3 个答案:

答案 0 :(得分:2)

执行以下操作

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> 

答案 1 :(得分:0)

将其添加到数组索引中的字符串。确保您的阵列密钥是USD或USB:

$amount_output = $amount_input*$currencies["$currency1"]["$currency2"];

对于测试代码,您可以测试HERE这个完整的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);

答案 2 :(得分:0)

您选择类型中的默认选项如下所示:

currency_type_from  EUR
currency_type_to    EUR

$currencies中不存在数组键'EUR',因此您得到未定义的索引错误