开发者

How should I convert country codes?

开发者 https://www.devze.com 2023-03-10 16:25 出处:网络
I\'m storing addresses with country codes according to ISO 3166 3-letter country codes.If I want to display the unabbreviated format, what 开发者_运维问答is the best OO way to do conversions like this

I'm storing addresses with country codes according to ISO 3166 3-letter country codes. If I want to display the unabbreviated format, what 开发者_运维问答is the best OO way to do conversions like this?


It all boils down to a JOIN: you need to have a countries table with the country code as the primary key, plus a name column, and join that on the original table. This is just basic database design, nothing to do with OOP.

An alternative would be to do two queries, which is only feasible if you need no more than a handful of countries - otherwise, the number of queries will skyrocket.

It then depends on your ORM how you integrate this with your OOP code; you may have to wrap the joined query in a view (which is probably good practice anyway).


Create a table called countries, it would store the 3 letter code ( which can be the primary key ) and then store the full name

so the table structure might look something like this:

short_name | full_name

OR You can create an array which contains the short name as the key and full name as the value something like:

$countries['USA'] = 'United States of America';
$countries['JPN'] = 'JAPAN';
....

and then look the full name using this array which can be in an include file, which you can easily re-use.


Include in your database the countries with their full title and country codes.

Then just query your database. You could design an object to help you.

0

精彩评论

暂无评论...
验证码 换一张
取 消