TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-09-2009, 02:21 AM   #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,210
Thanks: 90
Wildhoney is on a distinguished road
Box Currency Converter with Cache

I thought I would release my currency converter to the unsuspecting public, as it is quite useful, and also for people to critique.

Script uses the following XML document to update a local cache file periodically. All currencies in that XML document are against the Euro (€).

The cache expiration can be modified by changing the class constant CACHE_TIMEOUT, which is an integer representing the hours. The cache file's location can also be changed by altering the two cache constants CACHE_NAME, and its path as set in CACHE_DIR.

I have created the class a singleton and so all calls to the class must first pass through the getInstance method.

A simple example on how to use the class immediately can be seen by doing the following:

php Code:
printf('Conversion: £%01.2f', TalkPHP_Currency::getInstance()->getConvertedAmount(100, 'USD', 'GBP'));

Any invalid currencies specified in the second or third argument will throw an exception like the following, notifying you of valid currencies:

Quote:
Fatal error: Uncaught exception 'Exception' with message 'Currency "USDN/A" is invalid. Available: USD, JPY, BGN, CZK, DKK, EEK, GBP, HUF, LTL, LVL, PLN, RON, SEK, CHF, NOK, HRK, RUB, TRY, AUD, BRL, CAD, CNY, HKD, IDR, INR, KRW, MXN, MYR, NZD, PHP, SGD, THB, ZAR' in C:\wamp\www\Currency.php:166 Stack trace: #0 C:\wamp\www\Currency.php(182): TalkPHP_Currency->_validateCurrency(Array) #1 C:\wamp\www\Currency.php(230): TalkPHP_Currency->getConvertedAmount(100, 'USDN/A', 'GBP') #2 {main} thrown in C:\wamp\www\Currency.php on line 166
The arguments should be read in the following order: (Amount to convert, source currency, target currency).

You can check if you're currently using the cache by calling the isUsingCache method. This will return a boolean to let you know if the cache has been used for that particular call.

To return every single conversion rate against the Euro (€) there is the getConversionRates method. There is also a more specific function for returning a single currency's value against the Euro (€) -- getConversionRate, which accepts the currency abbreviation (USD, GBP, etcetera...).

In the downloadable file there are examples at the bottom of the document for help in using this class. However, it is fairly straightforward as it is, and really shouldn't cause anybody any issues. Just download and begin to convert currencies to other currencies immediately!

This is the comprehensive list of supported currencies (Both to and from):
  • United States Dollar (USD)
  • Japanese Yen (JPY)
  • Bulgarian Lev (BGN)
  • Czech Koruna (CZK)
  • Danish Krone (DKK)
  • Estonian Kroon (EEK)
  • Great British Pound (GBP)
  • Hungarian Forint (HUF)
  • Lithuanian Litas (LTL)
  • Latvian Lats (LVL)
  • Polish Zloty (PLN)
  • Romanian Lei (RON)
  • Swedish Krona (SEK)
  • Swiss Franc (CHF)
  • Norwegian Krone (NOK)
  • Croatian Kuna (HRK)
  • Russian Ruble (RUB)
  • Turkish Lira (TRY)
  • Australian Dollar (AUD)
  • Brazil Real (BRL)
  • Canadian Dollar (CAD)
  • Chinese Yuan (CNY)
  • Hong Kong Dollar (HKD)
  • Indonesian Rupiah (IDR)
  • Indian Rupee (INR)
  • South Korean Won (KRW)
  • Mexican Peso (MXN)
  • Malaysian Ringgit (MYR)
  • New Zealand Dollar (NZD)
  • Philippine Peso (PHP)
  • Singapore Dollar (SGD)
  • Thai Baht (THB)
  • South African Rand (ZAR)

Incidentally, notice there's a currency with the abbreviation "PHP" (Philippine Peso) !
Attached Files
File Type: php TalkPHP_Currency.php (6.5 KB, 72 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following 2 Users Say Thank You to Wildhoney For This Useful Post:
codefreek (06-10-2009), sketchMedia (06-09-2009)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a Simple Currency Converter with Automatic Symbols Wildhoney General 10 12-02-2009 12:20 AM
Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 02:53 PM
A bug or a cache problem?! yazid Advanced PHP Programming 0 05-22-2008 09:40 AM
Cache or not to cache? Brook Absolute Beginners 5 12-08-2007 04:53 PM
How do you 'cache' a page? Brook General 1 02-27-2007 03:36 PM


All times are GMT. The time now is 02:53 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design