Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Customising the CMS /

Get the country name from the 2 digit country code


Reply


4 Posts   1035 Views

Avatar
Fraser

Community Member, 47 Posts

3 May 2012 at 12:50pm

Is there anyway I can easily get the country name from the 2 letter country code.

I am populating a dropdown from the values stored in a table for country, however these are stored as NZ, US, etc. Is there a simple way I can do this without populating a table with code/name matches and doing a join?

//get countries
      $countryMap = array();
      $countryMap[""] = "ALL COUNTRIES";
      $sql = new SQLQuery();
      $sql->select = array(
         'Country'
      );
      $sql->from = array(
         'Member'
      );
      $sql->where = array(
         "Country !='' AND Country IS NOT NULL"
      );
      $sql->orderby('Country');

      $result = $sql->execute();

      foreach($result as $row){
         $countryMap[$row['Country']] = $row['Country'];
      }
      

      return new FieldSet(
         new LiteralField('Countries',"<b>Selected Countries:".$this->MemberCountry."</b>"),
         new ListboxField('MemberCountry','Country',$countryMap,'',null,true)         
      );

Avatar
Willr

Forum Moderator, 5513 Posts

4 May 2012 at 6:40pm

Geoip::countryCode2name() might be what you want.

http://api.silverstripe.org/2.4/sapphire/misc/Geoip.html#methodcountryCode2name

Avatar
hpeide

Community Member, 3 Posts

21 November 2012 at 1:43am

Is there a Geoip::countryCode2name around in Silverstripe 3.0?

Avatar
hpeide

Community Member, 3 Posts

23 November 2012 at 10:59pm

Edited: 23/11/2012 11:16pm

Possibly a better way doning this, but this works in SS3:

public function getCMSFields()
{
$f = parent::getCMSFields();

$f->addFieldToTab("Root.Main", new CountryDropdownField("VisitCountry", _t("SOME_PROJECT.Country", "Country")), "Metadata");

return $f;
}

public function CountryNice()
{
$locale = Zend_Locale::getLocaleToTerritory($this->VisitCountry);
return Locale::getDisplayRegion($locale, i18n::get_locale());

return Zend_Locale::getTranslation($this->VisitCountry, "country", i18n::get_locale())
}