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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Get the country name from the 2 digit country code


Go to End


4 Posts   3443 Views

Avatar
Fraser

Community Member, 48 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, 5523 Posts

4 May 2012 at 6:40pm

Avatar
hpeide

Community Member, 6 Posts

21 November 2012 at 1:43am

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

Avatar
hpeide

Community Member, 6 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())
}