Currency Convertor Tutorial
Ever wanted to know how much something was in another currency? Now you can with this short and sweet piece of code?
This tutorial is very simple to implement and has endless uses. The biggest benefit will be to those running e-commerce stores who ship worldwide and want to get the latest exchange rates. The method uses Web Services to consume the latest exchange rate into your application. A web service is simply data that can be gathered and manipulated by your application.
The converter uses a free web service from www.xmethods.net which supplies literally thousands of different web services that you can just gobble up. For now though we are only interested in this one: http://www.xmethods.net/ve2/ViewListing.po?key=uuid:D784C184-99B2-DA25-ED45-3665D11A12E5
To get going, create a new page called exchange.cfm
Here's the code in full:
<form action="exchange.cfm?do" method="post" name="form">
Change this amount:
<input type="text" name="amount">
into
<select name="currency" size="1">
<option value="united states">US dollars</option>
<option value="euro">Euros</option>
</select>
<br><br>
<input type="submit" value="Convert it!">
</form>
<p> </p>
<cfif IsDefined("url.do")>
<cfinvoke
webservice="http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl"
method="getRate"
returnvariable="aRate">
<cfinvokeargument name="country1" value="united kingdom"/>
<cfinvokeargument name="country2" value="#form.currency#"/>
</cfinvoke>
<cfoutput>
You entered <b>#form.amount#</b> which is worth #NumberFormat(Evaluate(amount * aRate), '.__')# in #form.currency# currency
</cfoutput>
</cfif>
OK lets break it down into chunks:
<form action="exchange.cfm?do" method="post" name="form">
Change this amount:
<input type="text" name="amount">
into
<select name="currency" size="1">
<option value="united states">US dollars</option>
<option value="euro">Euros</option>
</select>
<br><br>
<input type="submit" value="Convert it!">
</form>
This just sets up a simple form for you to enter the value you want to change and choose the currency you want to change it to. The full list of countries is show at the same location as the web service above.
<cfif IsDefined("url.do")>
<cfinvoke
webservice="http://www.xmethods.net/sd/2001/CurrencyExchangeService.wsdl"
method="getRate"
returnvariable="aRate">
<cfinvokeargument name="country1" value="united kingdom"/>
<cfinvokeargument name="country2" value="#form.currency#"/>
</cfinvoke>
OK the code here checks to see if the form has been submitted and calls the web service using the CFINVOKE tag. The two arguments are the currency you are converting from and to. The latter is set by our form.
<cfoutput>
You entered <b>#form.amount#</b> which is worth #NumberFormat(Evaluate(amount * aRate), '.__')# in #form.currency# currency
</cfoutput>
</cfif>
Finally we display the conversion by multiplying our entered amount by the exchange rate and format the number so we don't get loads of trailing zeros.
That's it! You can take this forward by adding more currencies, save the rate in a session var to stop multiple request, etc but for now you have a working currency converter!
This tutorial will show you how to quickly and easily backup and restore a MySQL database using just ColdFusion scripts and avoiding having to use CFEXECUTE which is normally disallowed on shared hosting platforms.
ColdFusion 8 has some handy tags that will provide database detail but what if you're running CF7? Normally you would have to resort to running the mysqldump command but this is restrictive if you wanted to provide your users with a method to backup their databases.