2014-03-06 54 views
0

我是PHP的新手,所以我不確定我做錯了什麼,但它似乎只有最後,如果執行語句是美元兌美元,無論我選擇什麼貨幣。使用PHP的貨幣轉換器

我需要使它轉換FROM和TO幣種,我選擇和轉換得到顯示在amount_output框中。而且貨幣和我輸入的座標仍保留在結果中,而不會重置。

任何幫助它非常感謝。

<?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> 
+0

使用轉換率的2-d陣列會輕鬆很多 –

+0

或鹼在單個陣列 – Sparkup

回答

5

由於您是PHP的新手,我並不試圖向您展示您的貨幣轉換器的複雜方式。現在你需要學習基礎知識,而不應該專注於使用高級策略來簡化代碼。您的代碼是一個很好的一個,將很好地工作,如果兩項調整是由:

  1. 使用==(比較運算),而不是=(賦值運算符),當你比較兩個值。
  2. 使用if/else if/else結構而不是一系列不連貫的if結構。

現在你的代碼變成:

<?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; 
} 
?> 

編碼愉快!

+0

親愛downvoter對美元的所有貨幣,可我知道什麼是錯我的回答? –

1

腳本可以長得非常容易使用數組,你就不會有這麼多的指令得到的問題:

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

       $currencies = array(); 
       $currencies['CAD'] = array(
        'CAD' => 1, 
        'EUR' => 0.624514066, 
        'GBP' => 0.588714763, 
        'USD' => 0.810307 
       ); 

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

?> 
0

這將是更容易維護,基地對美元的所有貨幣然後執行以下操作:

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

$currencies = array(
    'USD' => 1, 
    'CAD' => 1.234100162, 
    'EUR' => 0.772200772, 
    'GDP' => 0.726532984 
); 

$amount_output = ($amount_input/$currencies[$currency1])*$currencies[$currency2];