Postal Code listing in PHP/JSON/MySQL -
i have database of ~980k postal codes, , form user can input either zip code, city, state, and/or country. i'd create script auto populates other fields based upon results of another, before arrive there i'm curious best manner parse amount of data select box form. not appropriate way? have on code far, looking pseudo code or thoughts.
database looks
so took recommendation sakamaki izayoi , found script modified work application. once reaches 3 characters searches database of ~980k postal codes , returns results instantly.
let me know guys's thoughts.
form
<input type="text" id="quote_shipper_postalcode" name="quote_shipper_postalcode" class="form-control" maxlength="5">
javascrpit
$(document).ready(function(){ var ac_config = { source: "ajax/ajax_postal_codes.php", select: function(event, ui){ $("#quote_shipper_city").val(ui.item.city); $("#quote_shipper_state").val(ui.item.state); $("#quote_shipper_postalcode").val(ui.item.zip); $("#quote_shipper_country").val(ui.item.country); }, minlength:3 }; $("#quote_shipper_postalcode").autocomplete(ac_config);
});
ajax script
<?php include_once('../config/config.php'); // cleaning term $term = trim(strip_tags($_get['term'])); //search database first 3 digits make not such huge query $zip_3 = $term; // rudimentary search $matches = array(); foreach(list_postal_codes($db_pdo,$zip_3) $zip){ if(stripos($zip['zip'], $term) == false){ $zip['value'] = $zip['zip']; $zip['label'] = "{$zip['zip']}, {$zip['city']} {$zip['state']} {$zip['country']}"; $matches[] = $zip; } } if ($matches == false) { $matches[] = $zip_3 . " not found"; } $matches = array_slice($matches, 0, 15); print json_encode($matches);
database query
function list_postal_codes($db_pdo,$zip_3) { //zip 3 first 3 digits of zip return $db_pdo->query("select `postal_code_city` `city`,`postal_code_state` `state`,`postal_code_ctry` `country`, `postal_code_code` `zip` `postal_codes` `postal_code_code` '" . $zip_3 . "%'"); }
blockquote
Comments
Post a Comment