City & State Search
Posted: Tue Oct 13, 2009 9:49 am
Question: How do I split a string containing a city and state without needing a comma to differentiate the city and state?
Project Details
I am developing a website which searches based on city and state, or zipcode. The zipcode works fine by it's self, I also have the search work with "city, st" or "city, state" where php looks for the comma and breaks it into city and state at that point.
My database contains a table using a 3rd party php class (http://www.micahcarrick.com/04-19-2005/ ... ation.html), it consists of rows which contain the city, state, st, zipcode, long, lat.
The on another table I have the actually listings with the city, state, and zipcode
So when I search I detect if it's either a zipcode or "city, st" or "city, state". If it's a city, st or city, state, I split along the comma, match the database for the city column and state column and then find the zipcode and continue processing my "search by distance".
I would like to be able to detect the city and state and split them without needing a comma in the string.
example search "tampa, fl" , "tampa, florida" (works) || "tampa fl", "tampa florida" (doesn't return results)
Code for results
Project Details
I am developing a website which searches based on city and state, or zipcode. The zipcode works fine by it's self, I also have the search work with "city, st" or "city, state" where php looks for the comma and breaks it into city and state at that point.
My database contains a table using a 3rd party php class (http://www.micahcarrick.com/04-19-2005/ ... ation.html), it consists of rows which contain the city, state, st, zipcode, long, lat.
The on another table I have the actually listings with the city, state, and zipcode
So when I search I detect if it's either a zipcode or "city, st" or "city, state". If it's a city, st or city, state, I split along the comma, match the database for the city column and state column and then find the zipcode and continue processing my "search by distance".
I would like to be able to detect the city and state and split them without needing a comma in the string.
example search "tampa, fl" , "tampa, florida" (works) || "tampa fl", "tampa florida" (doesn't return results)
Code for results
Code: Select all
//remove space from city, state after comma
$zipSearch = str_replace(", ",",",$zipSearch);
/////////////////////////////////////////// city, state lookup /////////////////////////////////////////
//begin city, state lookup if variable is not a 5 digit number
if(!is_numeric($zipSearch)) {
//divide city,state between comma
list($zipCity, $zipState) = split('[,]',$zipSearch);
//capitalize the city
$zipCity = ucwords($zipCity);
//check if state name was used
if(strlen($zipState)>3) {
//Capitalize a state
$zipState = ucwords($zipState);
// search database for zipcode based on city, state
mysql_select_db($database_goge, $goge);
$query_zipcodes = "SELECT * FROM zip_code WHERE city = '$zipCity' AND state_name = '$zipState'";
$zipcodes = mysql_query($query_zipcodes, $goge) or die(mysql_error());
$row_zipcodes = mysql_fetch_assoc($zipcodes);
$totalRows_zipcodes = mysql_num_rows($zipcodes);
}else{
////// if state abreviation was used
//all caps
$zipState = strtoupper($zipState);
//search database for city, st for zipcode match
mysql_select_db($database_goge, $goge);
$query_zipcodes = "SELECT * FROM zip_code WHERE city = '$zipCity' AND state_prefix = '$zipState'";
$zipcodes = mysql_query($query_zipcodes, $goge) or die(mysql_error());
$row_zipcodes = mysql_fetch_assoc($zipcodes);
$totalRows_zipcodes = mysql_num_rows($zipcodes);
}
// reapply returned zipcode to original search variable
$zipSearch = $row_zipcodes['zip_code'];
// set range to 50 miles around city origin zipcode
$range = "50";
}
//////////////// end convert city, state to zipcode /////////////////////////////