douzhui1972 2014-03-06 00:22
浏览 46
已采纳

使用PHP的货币转换器

I am new to PHP so i am not sure what i am doing wrong but it seems only last if statement is getting executed which is USD to USD no matter what currencies i pick.

I need to make it so it converts FROM and TO currency that i pick and the conversion gets displayed in the amount_output box. And that currencies and the mount i typed in remain in the results without getting reset.

Any help it greatly appreciated.

<?php                
$amount_input = filter_input(INPUT_POST, 'amount_input');
$currency1 = filter_input(INPUT_POST, 'currency1');
$currency2 = filter_input(INPUT_POST, 'currency2');

if ($currency1="CAD" AND $currency2="CAD")
{
    $amount_output = $amount_input*1.0;
}

if ($currency1="CAD" AND $currency2="EUR")
{
    $amount_output = $amount_input*0.624514066;
}       

if ($currency1="CAD" AND $currency2="GBP")
{
    $amount_output = $amount_input*0.588714763;
}       

if ($currency1="CAD" AND $currency2="USD")
{
    $amount_output = $amount_input*0.810307;
}       
if ($currency1="EUR" AND $currency2="CAD")
{
    $amount_output = $amount_input*1.601244959;
}

if ($currency1="EUR" AND $currency2="EUR")
{
    $amount_output = $amount_input*1.0;
}       

if ($currency1="EUR" AND $currency2="GBP")
{
    $amount_output = $amount_input*0.942676548;
}       

if ($currency1="EUR" AND $currency2="USD")
{
    $amount_output = $amount_input*1.2975;
}

if ($currency1="GBP" AND $currency2="CAD")
{
    $amount_output = $amount_input*1.698615463;
}

if ($currency1="GBP" AND $currency2="EUR")
{
    $amount_output = $amount_input*1.060809248;
}       

if ($currency1="GBP" AND $currency2="GBP")
{
    $amount_output = $amount_input*1.0;
}       

if ($currency1="GBP" AND $currency2="USD")
{
    $amount_output = $amount_input*1.3764;
} 

if ($currency1="USD" AND $currency2="CAD")
{
    $amount_output = $amount_input*1.234100162;
}

if ($currency1="USD" AND $currency2="EUR")
{
    $amount_output = $amount_input*0.772200772;
}       

if ($currency1="USD" AND $currency2="GBP")
{
    $amount_output = $amount_input*0.726532984;
}       

if ($currency1="USD" AND $currency2="USD")
{
    $amount_output = $amount_input*1.0;
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>F/X Calculator</title>
    <link href="fxCalcStyles.css" rel="stylesheet" type="text/css" />

</head>
<body>        
    <h1>Money Banks F/X Calculator</h1>
    <hr></hr>
    <form action="fxCalc.php" method="post">                
        <select name="currency1">
            <option value="CAD">CAD</option>
            <option value="EUR">EUR</option>
            <option value="GBP">GBP</option>
            <option value="USD">USD</option>
        </select>
        <input name="amount_input" type="text"/>
        <select name="currency2">
            <option value="CAD">CAD</option>
            <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 ?>" readonly="readonly"/>

        <br />

        <input type="submit" name="submit" value="Convert"></input>
        <input type="reset" name="reset" value="Reset"></input>

    </form>                        
</body>
</html>
  • 写回答

3条回答 默认 最新

  • doupu3635 2014-03-06 00:24
    关注

    Since you’re new to PHP, I’m not trying to show you complicated ways for your currency converter. Right now you need to learn the basics and shouldn’t focus on making your code concise by using advanced strategies. Your code is a nice one and will work perfectly if two adjustments are made:

    1. Use == (comparison operator) instead of = (assignment operator) when you compare two values.
    2. Use an if/else if/else structure instead of a series of disjointed if structures.

    Now your code becomes:

    <?php
    $amount_input = filter_input(INPUT_POST, 'amount_input');
    $currency1 = filter_input(INPUT_POST, 'currency1');
    $currency2 = filter_input(INPUT_POST, 'currency2');
    
    if ($currency1 == "CAD" AND $currency2 == "CAD")
    {
        $amount_output = $amount_input*1.0;
    }
    
    else if ($currency1 == "CAD" AND $currency2 == "EUR")
    {
        $amount_output = $amount_input*0.624514066;
    }       
    
    else if ($currency1 == "CAD" AND $currency2 == "GBP")
    {
        $amount_output = $amount_input*0.588714763;
    }       
    
    else if ($currency1 == "CAD" AND $currency2 == "USD")
    {
        $amount_output = $amount_input*0.810307;
    }       
    
    else if ($currency1 == "EUR" AND $currency2 == "CAD")
    {
        $amount_output = $amount_input*1.601244959;
    }
    
    else if ($currency1 == "EUR" AND $currency2 == "EUR")
    {
        $amount_output = $amount_input*1.0;
    }       
    
    else if ($currency1 == "EUR" AND $currency2 == "GBP")
    {
        $amount_output = $amount_input*0.942676548;
    }       
    
    else if ($currency1 == "EUR" AND $currency2 == "USD")
    {
        $amount_output = $amount_input*1.2975;
    }
    
    else if ($currency1 == "GBP" AND $currency2 == "CAD")
    {
        $amount_output = $amount_input*1.698615463;
    }
    
    else if ($currency1 == "GBP" AND $currency2 == "EUR")
    {
        $amount_output = $amount_input*1.060809248;
    }       
    
    else if ($currency1 == "GBP" AND $currency2 == "GBP")
    {
        $amount_output = $amount_input*1.0;
    }       
    
    else if ($currency1 == "GBP" AND $currency2 == "USD")
    {
        $amount_output = $amount_input*1.3764;
    } 
    
    else if ($currency1 == "USD" AND $currency2 == "CAD")
    {
        $amount_output = $amount_input*1.234100162;
    }
    
    else if ($currency1 == "USD" AND $currency2 == "EUR")
    {
        $amount_output = $amount_input*0.772200772;
    }       
    
    else if ($currency1 == "USD" AND $currency2 == "GBP")
    {
        $amount_output = $amount_input*0.726532984;
    }       
    
    else if ($currency1 == "USD" AND $currency2 == "USD")
    {
        $amount_output = $amount_input*1.0;
    } 
    ?>
    

    Happy coding!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请问有会的吗,用MATLAB做
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 ARIMA模型时间序列预测用pathon解决
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示