Jump to content

Recommended Posts

Hi guys. I've got a web site where users can enter their toy collections, with various pieces of info about each toy, such as the price they paid for it. I'm trying to make the site as "international" as possible, so instead of just having a blank field and assuming dollars, I'd like the user to be able to select a type of currency from a drop-down list next to the text field where they can type in a number.

 

To do this, I need a list of:

1. All of the known currency codes

2. Conversion rates for each code

3. A currency symbol for each code (so I can display the info properly later, or even tie it into the form with a little js)

 

I figured this would be easy, but somehow there doesn't really seem to be an elegant solution to get me all 3 pieces. I discovered the Pear "Services_ExchangeRates" package, but all it does is query an xml page from the UN for a list of currency codes (ie. "USD") and their descriptive strings (ie. "US Dollar"). It has a rate conversion and a simple format function, but it's not a "smart" function--it knows nothing about the type of currency it's formatting, it merely puts as many digits after the decimal point as you specify, adds commas, etc. It has almost no functionality at all beyond that.

 

I read about the "money_format" and "number_format" functions in PHP, but these aren't much help, either. money_format requires you to set the locale before each call, which would be great if there were some way to iterate over locales, or relate them in some way to these common currency codes, but no such luck as far as I know.

 

I could just sit down and hard-code a big array with all of the information I need by hand, but I'm trying to get away from that kind of crap. I know that new currencies don't come and go very often, but I don't want to have to be responsible for them when they do. Where's the clean solution? Is there an elegant API or package somewhere that does everything I need? Why am I coming up totally blank when I search around for this? Surely I'm not the first person in the world who's tried to do something like this? Am I missing something totally obvious?

 

Thanks much, guys.

Link to comment
https://forums.phpfreaks.com/topic/148960-countries-and-currencies/
Share on other sites

No php function available. You need a database table of all currencies, iso codes, symbols, and current exchange rates you wish to use. Pull this information out to use in your drop lists. Exchange rates change all the time so you will need to write an update script that can get the data from an external source, that is unless you want to manually add the rates in.

Thanks for the reply, Neil. I should have been more clear--the exchange rate info is provided by the Services_ExchangeRates package (PEAR). The real sticky point is how to get the currency symbols (dollar signs, yen symbols, euro symbol, etc). As I understand it the currency code is usually--but not always--a concatenation of a country's ISO code plus the first letter of it's currency descriptor ("CAD" = "CA" + "D" for "dollar"). I assume that the locale codes are derived from the iso codes, but if they're not an exact match I can't weld them to the currency codes.

 

I'm aware there isn't a magical PHP function that does everything. But surely this is a wheel I don't need to re-invent. I just can't imagine every PHP coder who sits down to write some financial software gluing and pasting his own little "currencies of the world" table together for his own personal use. If that's what I have to do, okay, I guess, but it sounds like a really brittle, shaky solution.

 

Come on guys, any better leads?

 

No php function available. You need a database table of all currencies, iso codes, symbols, and current exchange rates you wish to use. Pull this information out to use in your drop lists. Exchange rates change all the time so you will need to write an update script that can get the data from an external source, that is unless you want to manually add the rates in.

Its easy to find all countries currency codes and symbols. Took me 2 seconds on Google. Like I said store them in a currency database table. If you have found a PEAR package to get you exchange rates then great. I would store them in the table along with the currency code and symbol. Write a script to get the exchange rate updated i.e. once per day.

 

http://www.xe.com/symbols.php

 

I would prefer to implement my own system but if you want guides then search Google. Heres one I found:

http://www.talkphp.com/general/1422-creating-simple-currency-converter-automatic-symbols.html

I appreciate the reply, but you're totally missing the point. Yes, I'm aware there are various references for finding out the currency symbol for a certain currency code. My point is that I don't want to have to construct the information by hand. The first link you posted came up in my initial searches as well, but it's just a static table. I would have to go through it one by one, cutting and pasting symbols and inserting them into my own usable reference, be it a static text file or database table. I've also already seen the "tutorial" you linked to, but it's not any better. His method requires that you construct by hand a list of locale codes. So we're right back where we started. I don't want to have to be an expert on locale codes (unless it's possible to come up with some metric for converting currency codes to locale codes--that's probably what I'll have to look into), or refer by eye to somebody's static table.

 

I want to write code to do the work for me. We're programmers, that's what we do. The exchange code I have is already smart enough to only fetch and update it's list of currency codes once a month, and the exchange rates once a day (though I'll probably update it only once a month as well--the conversion part of this is minor to my purpose). Whenever those currency codes are re-fetched, I want to also automatically derive the proper currency symbols. That way I don't have to worry about new currency symbols coming and going. It's just done for me. We're programmers, this is the way we're supposed to think about things.

 

Can any of you provide any insight? Thanks much...

 

Its easy to find all countries currency codes and symbols. Took me 2 seconds on Google. Like I said store them in a currency database table. If you have found a PEAR package to get you exchange rates then great. I would store them in the table along with the currency code and symbol. Write a script to get the exchange rate updated i.e. once per day.

 

http://www.xe.com/symbols.php

 

I would prefer to implement my own system but if you want guides then search Google. Heres one I found:

http://www.talkphp.com/general/1422-creating-simple-currency-converter-automatic-symbols.html

Look mate, what are you on about,

if your a programmer then you have to use,

what avalable from the internet, and then create and design your own work and functions.

 

what your asking just took me 10 minutes to do with the provided links so what the problem programmer.

 

most off the programmers i no, re design there own work not rely on 3rd party codes / scripts already written.

 

as a programmer we are very lucky we got the ability, to use all the information thrown at us for free from the .net, in the first place.

 

the whole i dear off becoming a good programmer is to code your own code's / scripts from scratch.

 

unless your a programmer with copy and past burning your mouse / keyboard.

 

ready made codes / scripts are there to learn from and understand how it works, then re design your own then hopefully pass it on.

 

 

 

Please stay on topic. This thread is about currency codes, it's not a debate about the  merits of writing your code versus using someone else's. Nowhere have I suggested I'm not prepared to write my own code, but if someone has already written an API or class that is maintained and de-bugged, I'd be a fool not to use it. That's what PEAR is all about. My issue is that to me, just transcribing a list of currency symbols by hand into a database or text file is *not* writing code. It's clumsy busy work that I would prefer to automate. Now if you can provide some insight or a real discussion of the issue I'd love to hear it, otherwise please stay out of the thread.

 

Look mate, what are you on about,

if your a programmer then you have to use,

what avalable from the internet, and then create and design your own work and functions.

 

what your asking just took me 10 minutes to do with the provided links so what the problem programmer.

 

most off the programmers i no, re design there own work not rely on 3rd party codes / scripts already written.

 

as a programmer we are very lucky we got the ability, to use all the information thrown at us for free from the .net, in the first place.

 

the whole i dear off becoming a good programmer is to code your own code's / scripts from scratch.

 

unless your a programmer with copy and past burning your mouse / keyboard.

 

ready made codes / scripts are there to learn from and understand how it works, then re design your own then hopefully pass it on.

On my own is right. I spent most of the day chasing after various leads and dead ends, and in the end there are 3 solutions to this problem:

 

1. Upgrade to PHP 5.3 and use this: http://php.oregonstate.edu/manual/en/numberformatter.formatcurrency.php

Not an option for me, I use a hosted server.

 

2. Write a parser and curl this:

http://www.xe.com/symbols.php

Fairly reasonable, but it would take another day, and it would depend on that site being around for a long time. I've emailed them asking about an xml feed, but I'm not going to hold my breath.

 

3. Just bite the bullet and do what every other poor bastard seems to do and just hand-code a table, cutting and pasting the symbols for as many countries as I can stand.

 

PHP will give you some locale info with a currency symbol in it, but you don't necessarily have a locale for every country in the world available to you, so you can't just iterate over locales and collect symbols.

 

I'm just completely baffled by this one--it seems like a big glaring hole.  Oh well. A brittle, kludgy hand-coded currency table it is. On to the next disaster...

 

Thanks, guys.

 

If you find a code library that can do what you are after then let us know. Another suggestion is to strip an open source ecommerce store open, it may contain multi currency functionality. Other than that you're on your own.

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.