Jump to content

currency conversion script


johnwayne77

Recommended Posts

i am working at a currency conversion script.

here is the form code:
[code]<html>
<body>
<form id="form1" name="form1" method="get" action="jam.php">
  <table width="64%" border="0" align="center" cellpadding="10" cellspacing="10">
    <tr>
      <td colspan="3"><div align="center">currency conversion </div></td>
    </tr>
    <tr>
      <td><div align="center">
        <label>
        amount:
        <input name="amount" type="text" id="amount" value="1" />
        </label>
      </div></td>
      <td><div align="center">
        <label>
        from:
        <select name="from" id="from">
          <option value="EUR">EUR</option>
          <option value="USD">USD</option>
          <option value="RON">RON</option>
        </select>
        </label>
      </div></td>
      <td><div align="center">
        to:
        <select name="to" id="to">
          <option value="EUR">EUR</option>
          <option value="USD">USD</option>
          <option value="RON">RON</option>
                        </select>
      </div></td>
    </tr>
    <tr>
      <td colspan="3"><div align="center">
        <label>
        <input type="submit" name="Submit" value="Submit" />
        </label>
      </div></td>
    </tr>
  </table>
</form>
</body>
</html>[/code]

and this is the php script code:

[code]<?php
$amount = $_GET["amount"];
$from = $_GET["from"];
$to = $_GET["to"];
$usd = file_get_contents("http://www.infovalutar.ro/azi/USD.bnr");
$eur = file_get_contents("http://www.infovalutar.ro/azi/EUR.bnr");
//$gbp = file_get_contents("http://www.infovalutar.ro/azi/GBP.bnr");
$ron = "1";
$usdat = "USD";
$eurat = "EUR";
//$gbpat = "GBP";
$ronat = "RON";
$ronusd = $ron / $usd; // 1 ron = ? usd
$roneur = $ron / $eur; // 1 ron = ? eur
$eurusd = $eur / $usd; // 1 euro = ? usd
$usdeur = $usd / $eur; // 1 usd = ? euro

if ( $to = $usdat ) {
if ( $from = $eurat ) {
$newamount = $amount * $eurusd;
echo $newamount;
}
else if ( $from = $usdat ) {
$newamount = $amount * 1;
echo $newamount;
}
else if ( $from = $ronat ) {
$newamount = $amount * $ronusd;
echo $newamount;
}}
else if ( $to = $eurat ) {
if ( $from = $usdat ) {
$newamount = $amount * $usdeur;
echo $newamount;
}
else if ( $from = $eurat ) {
$newamount = $amount * 1;
echo $newamount;
}
else if ( $from = $ronat ) {
$newamount = $amount * $roneur;
echo $newamount;
}}
else if ( $to = $ronat ) {
if ( $from = $usdat ) {
$newamount = $amount * $usd;
echo $newamount;
}
else if ( $from = $ronat ) {
$newamount = $amount * 1;
echo $newamount;
}
else if ( $from = $eurat ) {
$newamount = $amount * $eur;
echo $newamount;
}}
//echo $newamount;
//echo $usd;
?> [/code]

no matter what values i chose, it echoes me the result 1.29161917787 which is equal to '1.00 EUR = 1.29161917787 USD"

I don't understand why.

What am I missing?
Link to comment
https://forums.phpfreaks.com/topic/34608-currency-conversion-script/
Share on other sites

Well, I decided not to go through your code to find the error because all of it is totally unnecessary. To convert an amount from one rate to another you just need to multiple by (from_rate / to_rate). You could rewrite ALL of the PHP code above into just a few lines of code. I combined the form and processing pages into a single page for easy testing.

[code]<html>
<body>

<?php

if (isset($_GET['amount'])) {

  $amount = $_GET["amount"];
  $from = $_GET["from"];
  $to = $_GET["to"];

  $rate['RON'] = "1";
  $rate['USD'] = file_get_contents("http://www.infovalutar.ro/azi/USD.bnr");
  $rate['EUR'] = file_get_contents("http://www.infovalutar.ro/azi/EUR.bnr");
  //$rate['GBP'] = "1"; = file_get_contents("http://www.infovalutar.ro/azi/GBP.bnr");

  $newAmt = number_format( $amount * ($rate[$to]/$rate[$from]), 2 );
  echo $amount ." (" . $from . ") = " . $newAmt . " (" . $to . ")<br><br>";
}

?>


<form id="form1" name="test" method="GET" action="<?=$PHP_SELF?>">
  <table width="64%" border="0" align="center" cellpadding="10" cellspacing="10">
    <tr>
      <td colspan="3"><div align="center">currency conversion </div></td>
    </tr>
    <tr>
      <td><div align="center">
        <label>
        amount:
        <input name="amount" type="text" id="amount" value="<?=$amount?>" />
        </label>
      </div></td>
      <td><div align="center">
        <label>
        from:
        <select name="from" id="from">
          <option value="EUR" <?php if ($from=="EUR") echo " SELECTED"; ?>>EUR</option>
          <option value="USD" <?php if ($from=="USD") echo " SELECTED"; ?>>USD</option>
          <option value="RON" <?php if ($from=="RON") echo " SELECTED"; ?>>RON</option>
        </select>
        </label>
      </div></td>
      <td><div align="center">
        to:
        <select name="to" id="to">
          <option value="EUR" <?php if ($to=="EUR") echo " SELECTED"; ?>>EUR</option>
          <option value="USD" <?php if ($to=="USD") echo " SELECTED"; ?>>USD</option>
          <option value="RON" <?php if ($to=="RON") echo " SELECTED"; ?>>RON</option>
                        </select>
      </div></td>
    </tr>
    <tr>
      <td colspan="3"><div align="center">
        <label>
        <input type="submit" name="Submit" value="Submit" />
        </label>
      </div></td>
    </tr>
  </table>
</form>
</body>
</html>[/code]
  • 2 months later...
[quote author=thoma11 link=topic=122846.msg571381#msg571381 date=1176244359]
Can u send me the full script of currency conversion script?
[/quote]

What do you think that is above? Just modify the code to handle whatever currencies you want assuming that the conversion rate is available from infovalutar.ro
When I tried to run from command prompt it showing undefined from and to in these lines..
<option value="EUR" <?php if ($from=="EUR") echo " SELECTED"; ?>>EUR</option>
and
<option value="EUR" <?php if ($to=="EUR") echo " SELECTED"; ?>>EUR</option>.
But I already defined
$from = $_GET["from"];
  $to = $_GET["to"];
this one is right?
this is the whole php script (currency.php)

let me know if you need further assistance.

[code]<title>currency conversion v 1.0b by Ducoci</title>


<form id="form1" name="test" method="POST" action="<?=$PHP_SELF?>">
  <p>&nbsp;</p>
  <p align="center">&nbsp;</p>
  <table width="64%" border="1" align="center" cellpadding="10" cellspacing="10" bordercolor="#003366">
    <tr bgcolor="#006666">
      <td colspan="3" bgcolor="#006666"><div align="center"><font color="#FFFFFF"><strong>currency conversion</strong></font></div></td>
    </tr>
    <tr>
      <td><div align="center">
        <label>
        amount:
        <input name="amount" type="text" id="amount" value="<?=$amount?>" />
        </label>
      </div></td>
      <td><div align="center">
        <label>
        from:
        <select name="from" id="from">
          <option value="EUR" <?php if ($from=="EUR") echo " SELECTED"; ?>>EUR</option>
          <option value="USD" <?php if ($from=="USD") echo " SELECTED"; ?>>USD</option>
          <option value="RON" <?php if ($from=="RON") echo " SELECTED"; ?>>RON</option>
  <option value="GBP" <?php if ($from=="GBP") echo " SELECTED"; ?>>GBP</option>
  <option value="XAU" <?php if ($from=="XAU") echo " SELECTED"; ?>>XAU</option>
  <option value="XDR" <?php if ($from=="XDR") echo " SELECTED"; ?>>XDR</option>
  <option value="MDL" <?php if ($from=="MDL") echo " SELECTED"; ?>>MDL</option>
  <option value="CAD" <?php if ($from=="CAD") echo " SELECTED"; ?>>CAD</option>
  <option value="AUD" <?php if ($from=="AUD") echo " SELECTED"; ?>>AUD</option>
        </select>
        </label>
      </div></td>
      <td><div align="center">
        to:
        <select name="to" id="to">
          <option value="RON" <?php if ($to=="RON") echo " SELECTED"; ?>>RON</option>
          <option value="USD" <?php if ($to=="USD") echo " SELECTED"; ?>>USD</option>
          <option value="EUR" <?php if ($to=="EUR") echo " SELECTED"; ?>>EUR</option>
  <option value="GBP" <?php if ($to=="GBP") echo " SELECTED"; ?>>GBP</option>
  <option value="XAU" <?php if ($to=="XAU") echo " SELECTED"; ?>>XAU</option>
  <option value="XDR" <?php if ($to=="XDR") echo " SELECTED"; ?>>XDR</option>
  <option value="MDL" <?php if ($to=="MDL") echo " SELECTED"; ?>>MDL</option>
  <option value="CAD" <?php if ($to=="CAD") echo " SELECTED"; ?>>CAD</option>
  <option value="AUD" <?php if ($to=="AUD") echo " SELECTED"; ?>>AUD</option>  
          </select>
      </div></td>
    </tr>
    <tr>
      <td colspan="3"><div align="center">(currency rates updated daily according to BNR official exchange rates) <br />
        <em><font size="2">[ XAU = gr. aur ; XDR = gr. argint ; MDL = leu moldovenesc ]</font></em> </div></td>
    </tr>
    <tr>
      <td colspan="3"><div align="center">
        <label>
        <input type="submit" name="Submit" value="Convert" />
        </label>
      </div></td>
    </tr>
  </table>
  <p>
    <?php

if (isset($_POST['amount'])) {

  $amount = $_POST["amount"];
  $from = $_POST["from"];
  $to = $_POST["to"];

  $rate['RON'] = "1";
  $rate['USD'] = file_get_contents("http://www.infovalutar.ro/azi/USD.bnr");
  $rate['EUR'] = file_get_contents("http://www.infovalutar.ro/azi/EUR.bnr");
  $rate['GBP'] = file_get_contents("http://www.infovalutar.ro/azi/GBP.bnr");
  $rate['XAU'] = file_get_contents("http://www.infovalutar.ro/azi/XAU.bnr");
  $rate['XDR'] = file_get_contents("http://www.infovalutar.ro/azi/XDR.bnr");
  $rate['MDL'] = file_get_contents("http://www.infovalutar.ro/azi/MDL.bnr");
  $rate['CAD'] = file_get_contents("http://www.infovalutar.ro/azi/CAD.bnr");
  $rate['AUD'] = file_get_contents("http://www.infovalutar.ro/azi/AUD.bnr");     
  $newAmt = number_format( $amount * ($rate[$from]/$rate[$to]), 2 );
//  $newAmt = number_format( $amount * ($rate[$to]/$rate[$from]), 2 );
//  echo $amount ." (" . $from . ") = " . $newAmt . " (" . $to . ")<br><br>";


echo  "</p>";
echo "<p>&nbsp;</p>";
echo "<hr align='center' width='70%'/>";
echo "<p>&nbsp;</p>";
echo  "<table width='50%' border='1' align='center' cellpadding='10' cellspacing='10' bordercolor='#003399'>";
echo  "<tr>";
echo    "<td colspan='3' bgcolor='#CC3300'><div align='center'><font color='#FFFFFF'><strong>result</strong></font></div></td>";
echo    "</tr>";
echo    "<tr>";
echo      "<td width='45%'><div align='center'>"; echo $amount ." (" . $from . ")"; echo "</div></td>";
echo      "<td width='10%'><div align='center'> = </div></td>";
echo      "<td width='45%'><div align='center'>"; echo $newAmt . " (" . $to . ")"; echo "</div></td>";
echo    "</tr>";
echo  "</table>";
echo  "<p align='center'>&nbsp;    </p> ";

  ?>
</form>
</body>
</html>[/code]
[quote author=thoma11 link=topic=122846.msg571664#msg571664 date=1176269808]
When I tried to run from command prompt it showing undefined from and to in these lines..
<option value="EUR" <?php if ($from=="EUR") echo " SELECTED"; ?>>EUR</option>
and
<option value="EUR" <?php if ($to=="EUR") echo " SELECTED"; ?>>EUR</option>.
But I already defined
$from = $_GET["from"];
  $to = $_GET["to"];
this one is right?
[/quote]

I think this has something to do with you running it from a command prompt. because even if you didn't define those variables PHP wouldn't throw that error in that situation.
So at the moment its set to this;

  $rate['RON'] = "1";
  $rate['USD'] = file_get_contents("http://www.infovalutar.ro/azi/USD.bnr");
  $rate['EUR'] = file_get_contents("http://www.infovalutar.ro/azi/EUR.bnr");
  $rate['GBP'] = file_get_contents("http://www.infovalutar.ro/azi/GBP.bnr");
  $rate['XAU'] = file_get_contents("http://www.infovalutar.ro/azi/XAU.bnr");
  $rate['XDR'] = file_get_contents("http://www.infovalutar.ro/azi/XDR.bnr");
  $rate['MDL'] = file_get_contents("http://www.infovalutar.ro/azi/MDL.bnr");
  $rate['CAD'] = file_get_contents("http://www.infovalutar.ro/azi/CAD.bnr");
  $rate['AUD'] = file_get_contents("http://www.infovalutar.ro/azi/AUD.bnr"); 

so what if i want it to read from lets say the bankgok bank curreny rates which are located here;
http://www.bangkokbank.com/Bangkok+Bank/Web+Services/Rates/FX+Rates.htm

How do i set this?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.