Frage

I am building a site where there will be a bunch of companies and their business listing, so they will have their Address, City, State, Zip Code, etc.

I found this website here: http://jesseprice.com/mysql-city-state-zip-latitude-longitude-database/ which has a database of all city, state, zip, county, lat, long.

I imported the database above, so my question is how would I connect this to my companies table? And when a company signs up, I guess I could make a drop down for the state, and then it will drop down to the cities found in that state pulling from the database so they choose from the those, and then the zip code. Would that work? So they wouldn't have to enter it in themselves?

I'm just a little confused on how to do this properly and all make it work together.

Thank you.

War es hilfreich?

Lösung

Option 1:

Add ALL fields to your address table, referencing the selected table on additions, placing city, state, zip, county, lat and long into your table.

Advantages:

  • You can provide international support without making updates to the downloaded table.
  • You can update the downloaded table without loss of data (unlikely)
  • All data is available in one single-table query.
  • lat/lon searches should be faster.
  • Allows for flexibility in addresses that may not be available in the downloaded table.

Option 2:

Add a zip field only, and for all lookups, pull the data from the downloaded table.

Advantages:

  • Normalized - all records are in one table. Less drive space.

I would actually recommend adding the fields into your managed table, and keep the downloaded table as a read-only, replaceable reference table.

Andere Tipps

You need have following table design (companies/cities) -

CREATE TABLE `companies` (
  `companyID` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `companyName` varchar(150) NOT NULL DEFAULT '',
  `address` varchar(300) NOT NULL DEFAULT '',
  `cityID` smallint(5) NOT NULL,
  `countryID` varchar(3) NOT NULL,
  PRIMARY KEY (`companyID`),
  KEY `cityID` (`cityID`),
  KEY `countryID` (`countryID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;


CREATE TABLE `countries` (
  `countryID` varchar(3) NOT NULL DEFAULT '',
  `countryName` varchar(52) NOT NULL DEFAULT '',
  `localName` varchar(45) NOT NULL,
  `webCode` varchar(2) NOT NULL,
  `region` varchar(26) NOT NULL,
  `continent` enum('Asia','Europe','North America','Africa',
'Oceania','Antarctica','South America') NOT NULL,
  PRIMARY KEY (`countryID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `cities` (
  `cityID` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `cityName` varchar(50) NOT NULL,
  `stateID` smallint(5) unsigned NOT NULL DEFAULT '0',
  `countryID` varchar(3) NOT NULL DEFAULT '',
  PRIMARY KEY (`cityID`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

You can download full list of world cities / countries in mysql format from here -

http://myip.ms/info/cities_sql_database/World_Cities_SQL_Mysql_Database.html

.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top