function countries_api_csv_import_countries in Country codes API 6
Same name and namespace in other branches
- 5 countries_api.module \countries_api_csv_import_countries()
Function to import countries from CSV file TODO: provide arguments for specifying csv files TODO: setup permissions
Parameters
$offset: Int value for csv row offset.
2 calls to countries_api_csv_import_countries()
- countries_api_install in ./
countries_api.install - Implementation of hook_install().
- countries_api_update_6101 in ./
countries_api.install - Implementation of hook_update_N().
File
- ./
countries_api.module, line 320 - Countries API provides an API for official and up-to-date ISO 3166 country codes (alpha-2 and alpha-3) and names (official short names).
Code
function countries_api_csv_import_countries($offset = 1) {
//Prepopulate countries table
$handle = fopen(dirname(__FILE__) . "/data/countries.csv", "r");
$index = 1;
while (($row = fgetcsv($handle, 1024, ",")) !== FALSE) {
//Create row variables
$record = array(
COUNTRIES_API_FORMAT_ISO2 => $row[0],
COUNTRIES_API_FORMAT_NAME => $row[1],
COUNTRIES_API_FORMAT_PRINTABLE_NAME => $row[2],
COUNTRIES_API_FORMAT_ISO3 => $row[3] !== 'NULL' ? $row[3] : NULL,
COUNTRIES_API_FORMAT_NUMCODE => $row[4] !== 'NULL' ? $row[4] : NULL,
);
if ($index > $offset) {
db_query("INSERT INTO {countries_api_countries}\n (iso2, name, printable_name, iso3, numcode)\n VALUES('%s', '%s', '%s', '%s', '%s')", $record[COUNTRIES_API_FORMAT_ISO2], $record[COUNTRIES_API_FORMAT_NAME], $record[COUNTRIES_API_FORMAT_PRINTABLE_NAME], $record[COUNTRIES_API_FORMAT_ISO3], $record[COUNTRIES_API_FORMAT_NUMCODE]);
}
$index++;
}
fclose($handle);
// Work around a deficiency in db_query() (unable to insert NULL ints)
db_query("UPDATE {countries_api_countries} SET numcode = NULL WHERE numcode = 0");
watchdog('countries_api', "Pre-populated countries api data.");
}