Jump to:

5540 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Get the country name from the 2 digit country code

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

Page: 1
Go to End
Author Topic: 902 Views
  • Fraser
    Avatar
    Community Member
    46 Posts

    Get the country name from the 2 digit country code Link to this post

    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)         
          );

  • Willr
    Avatar
    Forum Moderator
    5503 Posts
  • hpeide
    Avatar
    Community Member
    3 Posts

    Re: Get the country name from the 2 digit country code Link to this post

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

  • hpeide
    Avatar
    Community Member
    3 Posts

    Re: Get the country name from the 2 digit country code Link to this post

    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())
    }

    902 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.