We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36805
    • 354 Posts
    I want to use the MODx countrylist files to populate a html select for signup/edit profile. This works fine however I cannot think of a way to have this appear in alphabetically sorted order.

    MODx comes with countrylists in manager/includes/lang/country.

    In the English version these are, more or less, sorted. In other language they are not.

    an excerpt of the german one looks like this
    $_country_lang["49"] = 'Kongo, Republik von'; // Congo
    $_country_lang["52"] = 'Cote D&39;Ivoire'; // Cote D&39;Ivoire
    $_country_lang["53"] = 'Kroatien'; // Croatia
    $_country_lang["54"] = 'Kuba'; // Cuba
    $_country_lang["55"] = 'Zypern'; // Cyprus
    $_country_lang["56"] = 'Tschechische Republik'; // Czech Republic

    The same excerpt looks like this in english
    $_country_lang["49"] = 'Congo, Republic of the'; // Congo
    $_country_lang["52"] = 'Cote D'Ivoire'; // Cote D'Ivoire
    $_country_lang["53"] = 'Croatia'; // Croatia
    $_country_lang["54"] = 'Cuba'; // Cuba
    $_country_lang["55"] = 'Cyprus'; // Cyprus
    $_country_lang["56"] = 'Czech Republic'; // Czech Republic

    This is kind of hard to fix. Fetching countrylists from an sql statement would easily make your select dropdown options alphabetically sorted. But in MODx Evo I only have them in plain arrays.

    Only solutions I can think of is:

    • heavy preprocessing of the array in php
    • making it a table (very laborious)
    • sort it in javascript as a last resort
    Anyone here has another idea or knows a way to make the second alternative viable in a reasonable amount of time?
      • 36805
      • 354 Posts
      I decided to go the jquery route. I suppose it’s ok if it is only half sorted with javascript off. Still fetching the correct version is tedious. I use this class with hardcoded filenames to load the correct version.
      <?php
      
      /**
       * Description of CountryListLoader
       *
       * @author MrDutchy
       */
      class CountryListLoader
      {
          private $file = null;
          private $countryArray = null;
      
          function __construct($locale = 'de_DE')
          {
              switch ($locale)
              {
                  case 'de_DE':
                  case 'de_AT':
                  case 'de_CH':
                      $this->file = 'german-UTF8-country.inc.php';
                      break;
                  case 'en_GB':
                  case 'en_AU':
                  case 'en_US':
                  case 'en_CA ':
                      $this->file = 'english_country.inc.php';
                      break;
                  case 'fr_FR':
                  case 'fr_BE':
                  case 'fr_CH':
                      $this->file = 'french_country.inc.php';
                      break;
                  case 'es_ES ':
                      $this->file = 'spanish_country.inc.php';
                      break;
                  case 'it_CH':
                  case 'it_IT':
                      $this->file = 'italian_country.inc.php';
                      break;
                  case 'nl_NL':
                      $this->file = 'dutch_country.inc.php';
                      break;
                  default:
                      $this->file = 'german_country.inc.php';
                      break;
              }
              require MODX_BASE_PATH . 'manager/includes/lang/country/' . $this->file;
              $this->countryArray =& $_country_lang;
          }
      
          public function getArray()
          {
              return $this->countryArray;
          }
      
      }
      
      ?>