You are here

function countries_api_csv_import_countries in Country codes API 5

Same name and namespace in other branches
  1. 6 countries_api.module \countries_api_csv_import_countries()

Function to import countries from CSV file TODO: provide arguments for specifying csv files

Parameters

$offset: Int value for csv row offset.

Return value

void

2 calls to countries_api_csv_import_countries()
countries_api_install in ./countries_api.install
Implementation of hook_install().
countries_api_update_5101 in ./countries_api.install
Implementation of hook_update_N().

File

./countries_api.module, line 267

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(
      'iso2' => $row[0],
      'name' => $row[1],
      'printable_name' => $row[2],
      'iso3' => $row[3] !== 'NULL' ? $row[3] : NULL,
      '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['iso2'], $record['name'], $record['printable_name'], $record['iso3'], $record['numcode']);
    }
    $index++;
  }
  fclose($handle);
  watchdog('countries_api', t('Pre-populated countries api data.'));
}