We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47562
    • 113 Posts
    Quick question.

    Within the lexicon/country files, I see the comment "Array keys refer to the ID number that is saved in the database". To what table does this refer?

    I'm looking to have a static lookup for 2-character code tied to that same index...

    $_country_abbr["1"] = 'AF'; // Afghanistan

    Thanks.
      • 3749
      • 24,544 Posts
      I'm assuming that you want to get the country name from the two-letter code. I don't know what that comment refers to, but I think to get what you want, you'll have to make your own file from the table here: http://blog.plsoucy.com/2012/04/iso-3166-country-code-list-csv-sql/. Paste the data (minus the first line) into a file called countries.csv.

      To avoid doing it by hand, you'll have to write a small utility snippet to create a file something like this:

      $countryName['Af'] = Afghanistan;
      $countryName['AL'] = Albania;
      // etc.


      Then reference it with:

      include 'path/to/file';
      $country = $countryName['Af'];


      The utility snippet would look something like this:

      $fp = fopen('myCountryFile', 'w'); /* file being written */
      
      if (($handle = fopen("countries.csv", "r")) !== FALSE) {
          while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
              $line = "\$countryName['" . $data[0] . '] = "' . 
                  $data[1] . '";' . "\n";
              fwrite($fp, $line);
          }
          fclose($handle);
          fclose($fp);
      }


      If you want the French names, change $data[1] to $data[2].

      If you want the country names in a language other than English or French, you'll need to find another source for the data. [ed. note: BobRay last edited this post 12 years, 2 months ago.]
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 47562
        • 113 Posts
        Thanks Bob.

        Actually, I was just trying to figure out what to use to index my lookup table for the abbreviation, and thought that the same key as that used for the full country name would be a natural choice. Just can't seem to find the table/field of that key...
          • 3749
          • 24,544 Posts
          Me too. That's why I suggested your own table. As far as I can tell, the MODX files are not set up to let you get from the two-letter code to the country name.
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 47562
            • 113 Posts
            I'm using the country dropdown posted by octoris (https://forums.modx.com/thread/47708/populate-dropdown-selectbox-with-countries), and the client uses 2-letter codes for their actual option values in the form, so I was just looking to introduce a 'cl.abbrev' placeholder into the mix. I guess I'll just key off of the full country name...