I've been searching all over Drupal for the answer to this one.

When my users sign up I want them to choose the country that they are based in. I have set up a 'country' field in the user profile following this:

http://drupal.org/node/118544

What I would like is for the appropriate flag to show up with the country name. I have downloaded flags from http://www.famfamfam.com/lab/icons/flags/

I would then like this flag to show up both in a user's comments and on their profile page when other users click on their profile.

I have tried IP to Country http://drupal.org/node/45806 but this doesn't give you the option to show the country on the profile page and I don't want to show the country of the IP but rather the country the user themselves select since they travel a lot (e.g. a user may be normally stationed in Kenya but be on holiday in the UK. I want their flag to remain 'Kenya' even if they post something while in the UK).

I have also tried the location module http://drupal.org/project/location but that doesn't seem to allow to you to show a country flag.

Help!

Comments

ricmadeira’s picture

Sounds like something you'd have to program yourself... which would take working knowledge of PHP, MySQL, CSS and Drupal's internal workings. It sounds really easy to do (that's the advantage of working with something as flexible as Drupal), but... if you're a beginner at all these things, like me, it will take you plenty of work and study and trial and error.

WorldFallz’s picture

Yep, definitely sounds like you need to customize the theme in the profile and comment template files. If the values in the field line up nicely with the names of the graphic files (ie "USA" = "usa.png"), it should be fairly simple. Check out Customising the user profile layout to get you started. Once you grasp that, theming comments will follow a similar approach.

lejon’s picture

I think this is still a bit beyond me, though it must be quite simple as internationalisation modules and IP To Country module call up flag icons using the standard ISO two-letter country code.

Other suggestions welcome!

72dpi’s picture

I know this is an old topic, but I thought i would share one easy solution for those who wish to do this.

You will need to add a new user profile field, visit:
www.yourwebsite.com/admin/user/profile

I put mine in a category called "Personal Information"

Create a new "list selection" field

Add the following:

Title: Country
Form name: profile_country
Exlplaination: Which country do you live in?

Selection options:

Abkhazia
Afghanistan
Akrotiri
Aland
Albania
Algeria
American Samoa
Andorra
Angola
Anguilla
Antigua and Barbuda
Argentina
Armenia
Aruba
Ascension Island
Australia
Austria
Azerbaijan
Bahamas, The
Bahrain
Bangladesh
Barbados
Belarus
Belgium
Belize
Benin 
Bermuda
Bhutan
Bolivia
Bosnia and Herzegovina
Botswana
Brazil
Brunei
Bulgaria
Burkina Faso 
Burundi
Cambodia
Cameroon
Canada
Cape Verde
Cayman Islands
Central African Republic
Chad
Chile
China
Christmas Island
Cocos (Keeling) Island
Colombia
Comoros
Congo (Brazzaville)
Congo (Kinshasa)
Cook Islands
Costa Rica 
Cote d'Ivoire
Croatia
Cuba
Cyprus
Czech Republic
Denmark
Dhekelia
Djibouti
Dominica
Dominican Republic
Ecuador 
Egypt
El Salvador
Equatorial Guinea
Eritrea
Estonia 
Ethiopia
Falkland Islands
Faroe Islands
Fiji
Finland
France
French Polynesia
Gabon
Gambia, The
Georgia
Germany
Ghana
Gibraltar
Greece
Greenland
Grenada
Guam
Guatemala
Guernsey
Guinea
Guinea-Bissau
Guyana
Haiti
Honduras
Hong Kong
Hungary
Iceland
India
Indonesia
Iran
Iraq
Ireland
Isle of Man
Israel
Italy
Jamaica
Japan
Jersey
Jordan
Kazakhstan
Kenya
Kiribati
Korea (North)
Korea (South)
Kosovo
Kuwait
Kyrgyzstan
Laos
Latvia
Lebanon
Lesotho
Liberia
Libya 
Liechtenstein
Lithuania
Luxembourg
Macau
Macedonia
Madagascar
Malawi
Malaysia
Maldives
Mali
Malta
Marshall Islands
Mauritania
Mauritius
Mayotte
Mexico
Micronesia
Moldova
Monaco
Mongolia
Montserrat
Morocco
Mozambique
Myanmar
Nagorno-Karabakh
Namibia
Nauru
Nepal
Netherlands, the
Netherlands Antilles
New Caledonia
New Zealand
Nicaragua
Niger
Nigeria 
Niue
Norfolk Island
Northern Cyprus
Northern Mariana Islands
Norway
Oman
Pakistan
Palau
Palestine
Panama
Papua New Guinea
Paraguay
Peru
Philippines, the
Pitcairn Islands
Poland
Portugal
Puerto Rico
Qatar
Romania 
Russia
Rwanda
Saint Helena
Saint Kitts and Nevis
Saint Lucia
Saint Pierre and Miquelon
Saint Vincent and the Grenadines
Samoa
San Marino
Sao Tome and Principe
Saudi Arabia
Senegal
Serbia and Montenegro
Seychelles
Sierra Leone
Singapore
Slovakia
Slovenia
Solomon Islands 
Somalia
Somaliland
South Africa
South Ossetia
Spain
Sri Lanka
Sudan
Suriname
Svalbard
Swaziland
Sweden
Switzerland
Syria
Taiwan
Tajikistan
Tanzania
Thailand
Timor-Leste
Togo
Tokelau
Tonga
Transnistria 
Trinidad and Tobago
Tristan da Cunha
Tunisia
Turkey
Turkmenistan
Turks and Caicos Islands
Tuvalu
Uganda
Ukraine 
United Arab Emirates
United Kingdom 
United States
Uruguay
Uzbekistan
Vanuatu
Vatican City
Venezuela
Vietnam
Virgin Islands (British)
Virgin Islands (U.S.)
Wallis and Futuna
Western Sahara
Yemen
Zambia
Zimbabwe

Make sure this is Public field, content shown on profile page and on member list pages.
Make sure the User Must enter a value
Make sure it is visible in registration field

Now, you'll need to insert the following where you want it in your custom user pages template:

<div class="fields">Country: <?php
//check if image exists
$Flagname = "http://www.yourwebsite.com/files/image/country_flags/". check_plain($user->profile_country).".gif";
if($img = @GetimageSize($Flagname))
{
echo '<img src="'.$Flagname.'" alt="Icon represents '.check_plain($user->profile_country).'" width="16" height="11" />';
}
else
{
echo "";
}
?> <?php print check_plain($user->profile_country) ;?></div>

Note: Now, this is not awesome, and you will have to change the Famfam fam names from their ISO names, to the ones in the list above, there is a better way (which would be to use php to convert from a DB table), i just haven't done it... All it needs is a very simple bit of php. I'll try, but would prefer a pro to do it.

Note: where we store the images: http://www.yourwebsite.com/files/image/country_flags/
Change accordingly.

Now, get some lovely icons, i recommend FamFamFam, cos he is a legend, and his icons are great:
http://www.famfamfam.com/lab/icons/flags/

This works for me, hopefully for you too.

Taking it to the next level:

List of names and ISO's
http://unstats.un.org/unsd/methods/m49/m49alpha.htm

The following website has some great code which could be the basis of this mod. I honestly reckon this should be rolled into the core of Drupal, considering it is so useful, and Drupal is global. if there are issues with Copyrights for images, I am sure there is a set available with no restrictions.

All is needed is to convert some php to read the row where we stored the table name above. The example below is taken from:
http://27.org/isocountrylist/

@ $db = mysql_pconnect($hostname, $dbusername, $dbpasswd);
if (!$db) {
    print "Failed to connect to the database.  Please try again later.\n";
    exit;
}
mysql_select_db($dbname);
$sql = "select id,name from country order by iso";
if (!(@ $result = mysql_query($sql))) {
    print "There was an error running your query:<p>\n". $sql . "<p>\n";
}
print '<select name="country"><br>\n';
print '<option value="">  Choose One   ';
while ($row = mysql_fetch_row($result)) {
    $country_iso   = $row[0];
    $country_name  = $row[1];

    print "<option value=\"$country_iso\"";
    if ($_REQUEST["country"] == $country_iso) { print " selected"; }
    print "> $country_name <br>\n";
}
print '</select>\n';

I'll leave that for a php guru, anhow, i reckon this is way better than using a massive ip to flag, is quicker, and is foolproof.

Any php legends who are up for it, please go for it and post back.
Cheers...

Heihachi88’s picture

This doesn't works for Drupal 6.16 did actually the same steps as u described in this post... 72dpi, can u take a look for ur code in D6? Thanks

janis_lv’s picture

didn't work for me either :[