Forum Moderators: coopster
$zip_list = "AB10,AB11,AB25,AB24,AB16,AB12,AB15,AB22,AB13,AB14";
$addOnSQL.="RIGHT(zip, -3) IN ('" . $zip_list . "')";
if (!empty($_POST["start_zip"])) {
$zip = new zipcode_class;
//UK based site can be 6 or 7 alphanumeric
$zip->units = "k"; //set as kilometers
$zip->db_table = "post_code"; //set as post_code table for UK
$zip_code = substr($_POST["start_zip"], 0, -3);
//US based site is always 5 digits
//$zip->units = "m"; //set as miles
//$zip->db_table = "zip_code"; //set as zip_code table for US
//$zip_code = $_POST["start_zip"];
if (!empty($addOnSQL))
$addOnSQL.=" AND ";
if (!empty($_POST["zip_range"]))
$zips = $zip->get_zips_in_range($zip_code, $_POST["zip_range"], _ZIPS_SORT_BY_DISTANCE_ASC, true);
//print_r($zips);
if (empty($zips)) {
$addOnSQL.="zip = '" . $_POST["start_zip"] . "'";
} else {
// this fetches all the zip codes in the $zips variable
$zipcodes = array_keys($zips);
$zip_list = implode(",",$zipcodes);
//$addOnSQL.="zip IN (" . $zip_list . ")";
$addOnSQL.="right(zip, -3) IN ('" . $zip_list . "')";
//mid(@text, 1, length(@text)-3)
}
}
// this fetches all the zip codes in the $zips variable
$zipcodes = array_keys($zips);
//print_r($zipcodes);
if (!empty($addOnSQL)) {
$addOnSQL.=" AND ";
}
$zip_count = count($zipcodes);
$c=0;
foreach ($zipcodes as $onezip) {
//needs to be an OR
$addOnSQL.="zip LIKE '" . $onezip . "___'";
if ($c == $zip_count-1) {
break;
} else {
$addOnSQL.=" OR ";
}
++$c;
}
User input on form:
zip: AB10SYX
Value stored in database table column:
zip: AB10
$zip_code = substr(trim($_POST["start_zip"]), 0, 4);
WHERE SUBSTRING(zip, 0, 4) IN (<your comma separated list here>)and another thought is to speed up your searches by created a new 4 character index on that column in your classifieds table:
ALTER TABLE classifieds ADD INDEX(zip(5));