/** * Constructor. Must call static::setConnection before constructor. * * @param string $location zip code or city, state combination * * @throws \UnexpectedValueException */ public function __construct($location) { parent::__construct($location); $this->pg_table = 'maps'; if (is_null(static::$dbConnection)) { throw new \UnexpectedValueException('Expected database connection resource but got null.'); } $this->pg_conn = static::$dbConnection; }
public function action_location($zipcode) { $result = false; if (!empty($zipcode)) { try { $result = ZipCode::find_by_zip($zipcode); } catch (Exception $e) { } } return $this->response($result); }
private function handleZipCodeImport() { ini_set('memory_limit', '1024M'); if (class_exists("ZipCode")) { $zip_code = new ZipCode(); } if (isset($zip_code) && $zip_code instanceof ZipCode) { echo '<pre>ZIPCODE IMPORT DEBUG:<br/>'; try { $zip_code->setDataDir(dirname(__FILE__)); $zip_code->setMaxRecordsToParse(0)->parseZipCodeData(); $zip_code_records = $zip_code->getRecords(); } catch (Exception $e) { echo 'Caught exception: ' . $e->getMessage() . PHP_EOL; } if (!empty($zip_code_records) && is_array($zip_code_records)) { $table_name = $this->_spq_scm_zipcodes_table; foreach ($zip_code_records as $zip_code_record) { $insert = "INSERT INTO {$table_name} " . "(fips_code, zip, city, state, longitude, latitude, population, allocation_factor)" . " VALUES (" . sprintf("%s,", $this->_wpdb->escape($zip_code_record['fips_code'])) . sprintf("%s,", $this->_wpdb->escape($zip_code_record['zip'])) . sprintf("'%s',", $this->_wpdb->escape($zip_code_record['city'])) . sprintf("'%s',", $this->_wpdb->escape($zip_code_record['state'])) . sprintf("%s,", $this->_wpdb->escape($zip_code_record['longitude'])) . sprintf("%s,", $this->_wpdb->escape($zip_code_record['latitude'])) . sprintf("%s,", $this->_wpdb->escape($zip_code_record['population'])) . sprintf("%s", trim($this->_wpdb->escape($zip_code_record['allocation_factor']))) . ')'; $results = $this->_wpdb->query($insert); } } echo '</pre>'; } }
private function locationType($location) { if (ZipCode::isValidZip($location)) { return ZipCode::LOCATION_ZIP; } elseif (ZipCode::isValidCityState($location)) { return ZipCode::LOCATION_CITY_STATE; } else { return false; } }
<h3>More Information</h3> <ul> <li><a href="https://github.com/Quixotix/PHP-ZipCode-Class">PHP-ZipCode-Class</a> source code and downloads on Github.</li> <li>My blog post: <a href="http://www.micahcarrick.com/php5-zip-code-range-and-distance.html">PHP 5 Zip Code Range and Distance Calculation</a>.</li> </ul> <?php include 'zipcode.php'; // connect to the MySQL database with the zip code table mysql_connect('localhost', 'YOUR DB USER', 'YOUR PASSWORD'); mysql_select_db('YOUR DB NAME'); // you can instantiate ZipCode with a zip code or with city and state $portland = new ZipCode("97214"); $ventura = new ZipCode("Ventura, CA"); /* You can get the distance to another location by specifying a zip code, city/state string, or another ZipCode object. You can specify whether you want to get the distance in miles or kilometers. */ echo "<h2>Get the distance between 2 zip codes</h2>"; $distance1 = round($portland->getDistanceTo("98501"), 2); $distance2 = round($portland->getDistanceTo($ventura, ZipCode::UNIT_KILOMETERS), 2); $distance3 = round($portland->getDistanceTo("Salem, OR"), 2); echo "Zip code <strong>{$portland}</strong> is <strong>{$distance1}</strong> miles away from " . "zip code <strong>98501</strong><br/>"; echo "Zip code <strong>{$portland}</strong> is <strong>{$distance2}</strong> <em>kilometers</em> away from " . "the city <strong>{$ventura}</strong><br/>"; echo "Zip code <strong>{$portland}</strong> is <strong>{$distance3}</strong> miles away from " . "the city <strong>Salem</strong><br/>"; /* You can get all of the zip codes within a distance range from teh zip. Here we