PHP - Moved: Not Sure Where To Ask This But - Php Zip Code Locator ??
This topic has been moved to MySQL Help.
http://www.phpfreaks.com/forums/index.php?topic=323503.0 Similar TutorialsThis topic has been moved to Application Design. http://www.phpfreaks.com/forums/index.php?topic=321176.0 I have this script that works 9/10 of how it's suppose too. This does not calculate the correct distance for the results. Can anybody see where the problem lies? Here is the code Code: [Select] // see if our zip code has been posted, and if it is a 5 digit # if (isset($_POST['zip'])) { // remember to sanitize your inputs // this removes whitespace around the data and the next line makes sure its a 5 digit number $findzip = trim($_POST['zip']); if (preg_match("#^\d{5}$#",$findzip)) { include('inc/connect.php'); include('inc/functions.php'); // create a query that will select the zip code (if we can't find the user entered zip, we return an error) $query = "SELECT latitude,longitude,state,city FROM zipdata WHERE zip=".$findzip." LIMIT 1"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) { // grab the latitude, longitude,state and city from our result list($flat,$flon,$fstate,$fcity) = mysql_fetch_row($result); // now get all markets in the same state, or all markets if no states match // we want to reduce the amount of work the database has to do so we see if a state matches $query = "SELECT * FROM mrk WHERE state='".$fstate."'"; $result = mysql_query($query); if (mysql_num_rows($result) < 1) { $query = "SELECT * FROM mrk"; $result = mysql_query($query); } // now we process the markets to gather their data, //this result should not be empty, so I was lazy and didn't write an else case if (mysql_num_rows($result) > 0) { while($row = mysql_fetch_assoc($result)) { // first put all of the data for this market into an array, with the market id as the key $storeinfo[$row['id']] = $row; // get the store zip $dzip = $row['zip']; // query for the store's latitude and longitude $query = "SELECT latitude,longitude FROM zipdata WHERE zip=".$dzip." LIMIT 1"; $result2 = mysql_query($query); if (mysql_num_rows($result2) > 0) { list($dlat,$dlon) = mysql_fetch_row($result2); // now get the distance from the user entered zip $stores[$row['id']] = calcDist($flat,$flon,$dlat,$dlon); } } } asort($stores); print "<p>The Markets closest to you: $findzip</p><br>\n"; $maploc = "'You are here','$findzip',"; foreach($stores as $k=>$v) { $output .= "<h3 style='margin:0;padding:0'><b>".$storeinfo[$k]['company']."</b><br>(approx ".round($v)." miles)</h3>"; $output .= "<p style='margin:0 0 10px 0;padding:0'><a href='".$storeinfo[$k]['url']."'>".$storeinfo[$k]['url']."</a><br>"; $output .= $storeinfo[$k]['city']." ".$storeinfo[$k]['state'].", ".$storeinfo[$k]['zip']."</p>"; } echo $output; } } } Here is the function.php Code: [Select] function calcDist($flat, $flon, $dlat, $dlon) { $distance = sin(deg2rad($flat)) * sin(deg2rad($dlat)) + cos(deg2rad($flat)) * cos(deg2rad($dlat)) * cos(deg2rad($flon - $dlon)); $distance = (rad2deg(acos($distance))) * 69.09; return $distance; } Thank you in advance This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=349890.0 This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=352862.0 This topic has been moved to Beta Test Your Stuff!. http://www.phpfreaks.com/forums/index.php?topic=309601.0 This topic has been moved to Other Libraries and Frameworks. http://www.phpfreaks.com/forums/index.php?topic=334386.0 This topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=351710.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=323161.0 This topic has been moved to PHP Freelancing. http://www.phpfreaks.com/forums/index.php?topic=322196.0 This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=317538.0 This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=345912.0 This topic has been rewritten to mod_rewrite. http://www.phpfreaks.com/forums/index.php?topic=356324.0 (have I used that one before? I need to write these down somewhere) This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=355529.0 This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=330152.0 This topic has been moved to HTML Help. http://www.phpfreaks.com/forums/index.php?topic=358229.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=357903.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=306039.0 This topic has been moved to JavaScript Help. http://www.phpfreaks.com/forums/index.php?topic=334389.0 This topic has been moved to Third Party PHP Scripts. http://www.phpfreaks.com/forums/index.php?topic=354376.0 This topic has been moved to Miscellaneous. http://www.phpfreaks.com/forums/index.php?topic=326606.0 |