mits Posted May 6, 2010 Share Posted May 6, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/200950-php-currency-converter/ Share on other sites More sharing options...
premiso Posted May 7, 2010 Share Posted May 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/200950-php-currency-converter/#findComment-1054384 Share on other sites More sharing options...
teamatomic Posted May 7, 2010 Share Posted May 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/200950-php-currency-converter/#findComment-1054386 Share on other sites More sharing options...
mits Posted May 7, 2010 Author Share Posted May 7, 2010 thanks for your replies. the task is just an exercise. It won't involve a database. If there are any tutorials out there, then that would be great. or if someone could just explain the logic behind such a system, then i can work it out using PHP. Quote Link to comment https://forums.phpfreaks.com/topic/200950-php-currency-converter/#findComment-1054512 Share on other sites More sharing options...
teamatomic Posted May 7, 2010 Share Posted May 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/200950-php-currency-converter/#findComment-1054636 Share on other sites More sharing options...
mits Posted May 7, 2010 Author Share Posted May 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200950-php-currency-converter/#findComment-1054638 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.