johnwayne77 Posted January 17, 2007 Share Posted January 17, 2007 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 = ? euroif ( $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? Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 17, 2007 Share Posted January 17, 2007 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><?phpif (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] Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 17, 2007 Share Posted January 17, 2007 Ok, I see your error now. In all your IF statements you are using a single '=' to test. You need to use double equal signs, '==' - otherwise it is assinging the value to the variable. Quote Link to comment Share on other sites More sharing options...
johnwayne77 Posted January 17, 2007 Author Share Posted January 17, 2007 sweeet!i've tested both methods, it works just fine!thank you very much dude! Quote Link to comment Share on other sites More sharing options...
thoma11 Posted April 10, 2007 Share Posted April 10, 2007 Can u send me the full script of currency conversion script? Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 10, 2007 Share Posted April 10, 2007 [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 Quote Link to comment Share on other sites More sharing options...
thoma11 Posted April 10, 2007 Share Posted April 10, 2007 I tried the exact code ..but when I submit the button showing "You are not authorised to access this resource"..Anything more I have to do .?I am new in php ..Thanks.. Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 11, 2007 Share Posted April 11, 2007 Odd, it works perfectly fine for me. Did you copy and paste all of the code from the 2nd post into a php file? Is your web server properly set up with access to the location of that file and is PHP enabled? Quote Link to comment Share on other sites More sharing options...
thoma11 Posted April 11, 2007 Share Posted April 11, 2007 yes, I copied the 2nd one..web server etc is correct ...but this type of error...:)no idea... Quote Link to comment Share on other sites More sharing options...
thoma11 Posted April 11, 2007 Share Posted April 11, 2007 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 Link to comment Share on other sites More sharing options...
johnwayne77 Posted April 11, 2007 Author Share Posted April 11, 2007 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> </p> <p align="center"> </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> <?phpif (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> </p>";echo "<hr align='center' width='70%'/>";echo "<p> </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'> </p> ";} ?></form></body></html>[/code] Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 11, 2007 Share Posted April 11, 2007 [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. Quote Link to comment Share on other sites More sharing options...
thoma11 Posted April 11, 2007 Share Posted April 11, 2007 Hi When I tried to run Currency conversion script it's showing the follwing error.Bad request: probably tried to POST a non-executable URLI am using xitami server and saved in right path in web server ..Any suggestion?Thanks.. Quote Link to comment Share on other sites More sharing options...
paul2463 Posted April 11, 2007 Share Posted April 11, 2007 this might seem straight forward but did you save it in your server root as a PHP file not a HTML file?? Quote Link to comment Share on other sites More sharing options...
johnwayne77 Posted April 16, 2007 Author Share Posted April 16, 2007 maybe you should install apache..? Quote Link to comment Share on other sites More sharing options...
danlew Posted April 20, 2007 Share Posted April 20, 2007 Script seems to work for me,but what if i want to add more country currencies to this like the Thai Baht etc. how do i make extra currencies available in this script Quote Link to comment Share on other sites More sharing options...
johnwayne77 Posted April 20, 2007 Author Share Posted April 20, 2007 I think you need to use the same algorithm.As for more currencies, that depends on where you feed from the updated rates. It's all in the RSS Quote Link to comment Share on other sites More sharing options...
danlew Posted April 22, 2007 Share Posted April 22, 2007 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.htmHow do i set this? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.