Jump to content

Recommended Posts

Hi, i'm trying to build a currency converter which can cater for a range of currencies. Say about 40 different ones.

 

i was going to try using a multi-dimensional array, but that won't be very efficient for a system requiring lots of currencies.

 

what is the best way to go about doing this? and are there any tutorials out there which i can learn from.

 

PS: I want to build this myself from scratch so please don't lead me to libraries etc.

 

thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/200950-php-currency-converter/
Share on other sites

Well you have a couple different methods and items to think about. First up, do you want it to be "current" as exchange rates constantly change. If so you will need to fetch that data with cURL daily (cron script) and update your DB with the rates.

 

An array would be fine, if done properly. But better to have a DB backend, and just input into the db each exchange rate from a to b and back. So one setup for the DB could be:

 

exchange_rates
   type  to  rate_to   rate_from

 

Type would be say USD or GBP to would be what you want to convert it to, so GBP or USD, etc. then the rate_to is the rate to from USD to GBP and from is to go from GBP to USD.

 

Again this may not be the best, but is one way. Hope that gets you started 

I would suggest using a common denominator. That is one currency as the common, the US$ would be best for this. Then:

eu to yen would be

eu to US$

US$ to yen

Use this to grab to a file and make an array to use:

http://www.x-rates.com/d/USD/table.html

If this is for anything more than an exercise I would suggest updating your daily.

 

 

HTH

Teamatomic

You go to a page that has a conversion rate table. grab the table and parse it down to something like this for a file:

Argentine Peso|3.91192|0.255629

Australian Dollar|1.10592|0.904225

Botswana Pula|7.00771|0.1427

...

 

Then you loop the file building two drop downs, one uses the to factor the other  uses the from factor. You have a text box for the coversion amount. Whatever table you use dont forget to add the common currency as a 1:1 conversion

Then, using the factors:

us_d=number*c_to;

converted=us_d*c_from;

 

HTH

Teamatomic

it's OK, i have done it.

 

i used the following formula;

 

Using the GBP as the base currency

 

EUR to USD = 1.425

 

### formula ###

 

$base_from = GBP to EUR = 1.136

$base_to = GBP to USD = 1.620

$rate = $base_to / $base_from = 1.620 / 1.136 = EUR to USD = 1.425

 

 

this system will cater for a lot more currencies e.g. 50 as mentioned above. Just add the rate to the pound in the array.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.