Form: let user select their country (drop-down) and get the currency (php)

懵懂的女人 提交于 2019-12-13 16:14:56

问题


Currently I'm writing a short survey (html form) using php, mysql and jquery. I want the user to select their country from a drop-down list and then get the right currency (server side) so later on I can ask things referring to the right currency.

I really don't got a clear view on how to achieve this. I know I can find an up to date country list from: http://www.iso.org/iso/country_codes/iso_3166_code_lists.htm I could make into a php array but then?

http://snipplr.com/view/36437/php-country-code--to-html-currency-symbol-list/ Seems nice code, but I like to use something that is up to date.

Its no problem for me to use a mysql database, but it is a problem to install plug-ins/expansions (hosting won't allow it).

Does somebody knows a good (and maybe easy) way to achieve this?


回答1:


You can use CURL or file_get_contents() to read the content from the URL and is always updated.




回答2:


Not answering your question, but:

As of PHP5.3 the intl became a default. It contains NumberFormatter::formatCurrency() that does what your linked country_currency() is trying to do - only properly. If PHP5.3 is a viable minimum requirement (seeing that 5.2 is deprecated and not supported anymore) - use the intl functions.

With Locale::acceptFromHttp() you can check the browser's request headers to preselect the best matching locale.

Your ISO CountryCode list should still be helpful for a manual <select> on a certain level. But keep in mind that it's not quite accurate: Germany translates to de, which may not be specific enough seeing de_AT, de_CH, de_DE. Each of them may present Currency differently. €1,123.23, 1 123,23 €, and so on. You'll still need to know which currency you're processing, though. So you need the list of ISO country codes AND the map of countrycode to currency.




回答3:


PHP Intl's NumberFormatter accepts English as a language for any country. So just use en_ plus your country code.

echo (new NumberFormatter('en_DE', NumberFormatter::CURRENCY))
    ->getTextAttribute(NumberFormatter::CURRENCY_CODE); // EUR

echo (new NumberFormatter('en_RS', NumberFormatter::CURRENCY))
    ->getTextAttribute(NumberFormatter::CURRENCY_CODE); // RSD


来源:https://stackoverflow.com/questions/6673096/form-let-user-select-their-country-drop-down-and-get-the-currency-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!