Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 25th July 2006, 22:09
tagtekin tagtekin is offline
Junior Member
 
Join Date: Jul 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default PHP MySql Store locator by lat/lon coordinates

I have the following code I have spent almost a week to get it to work it worked for while with a inaccurate distances now i dotn get anything at all here is my script
Help would be great before I go crazy

Thanks
<?php
//designed to calculate closest range 50 miles of offices for HIR
//two db tables and one database connections requred
$getzip = $_GET['getzip'];

$dbh=mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("phpziplocator");

$query1 = "Select * from zip_code where zip_code= '" . $getzip . "'";
$zip1 = mysql_query($query1) or die(mysql_error());
$row1 = mysql_fetch_assoc($zip1);


$query2 = "Select * from offices";
$zip2 = mysql_query($query2) or die(mysql_error());
$row2 = mysql_fetch_assoc($zip2);

//$result = mysql_query($query2,$dbh);
//while($row3=mysql_fetch_array ($result)) {
//echo ($row3['office_name']) ,"<BR>";
//}

do {
if (distance($row1['lon'], $row1['lat'], $row2['lon'], $row2['lat']) <= 5) {

echo $row2['city'] . " --- " . $row2['state'] . " --- " . $row2['zip'] . " --- <b>" . distance($row1['lon'], $row1['lat'], $row2['lon'], $row2['lat']) . "</b> --- " . $row2['lat'] . " --- " . $row2['lon'] . "<br>";



}

} while ($row2 = mysql_fetch_assoc($zip2));

//echo distance($row1['lat'], $row1['lon'], $row2['lat'], $row2['lon'], "M") . " Miles<br>";

mysql_free_result($zip1);
mysql_free_result($zip2);

function distance($lat1, $lon1, $lat2, $lon2)
{
$lat1 = floatval($lat1);
$lon1 = floatval($lon1);
$lat2 = floatval($lat2);
$lon2 = floatval($lon2);

// Formula for calculating distances
// from latitude and longitude.
$dist = acos(sin(deg2rad($lat1))
* sin(deg2rad($lat2))
+ cos(deg2rad($lat1))
* cos(deg2rad($lat2))
* cos(deg2rad($lon1 - $lon2)));


$dist = rad2deg($dist);
$miles = (float) $dist * 69.1;

// To get kilometers, multiply miles by 1.61
$km = (float) $miles * 1.61;

// This is all displaying functionality
$display = sprintf("%0.2f",$miles);

//$display = sprintf("%0.2f",$miles).' miles' ;
//$display .= ' ('.sprintf("%0.2f",$km).' kilometers)' ;

return $display ;
}
?>
Reply With Quote
  #2  
Old 26th July 2006, 11:32
falko falko is offline
Super Moderator
 
Join Date: Apr 2005
Location: Lüneburg, Germany
Posts: 31,853
Thanks: 781
Thanked 1,558 Times in 1,477 Posts
Default

Quote:
Originally Posted by tagtekin
Code:
$query2 = "Select * from offices";
$zip2 = mysql_query($query2) or die(mysql_error());
$row2 = mysql_fetch_assoc($zip2);

//$result = mysql_query($query2,$dbh); 
//while($row3=mysql_fetch_array ($result)) {
//echo ($row3['office_name']) ,"<BR>"; 
//}

do {
     if (distance($row1['lon'], $row1['lat'], $row2['lon'], $row2['lat']) <= 5) {   
	   
echo $row2['city'] . " --- " . $row2['state'] . " --- " . $row2['zip'] . " --- <b>" . distance($row1['lon'], $row1['lat'], $row2['lon'], $row2['lat']) . "</b> --- " . $row2['lat'] . " --- " . $row2['lon'] . "<br>";     
     
	
	
	 }
	
  } while ($row2 = mysql_fetch_assoc($zip2));
I think the problem is that you do
$row2 = mysql_fetch_assoc($zip2);
twice...
__________________
Falko
--
Follow me on:
Reply With Quote
  #3  
Old 26th July 2006, 11:43
Ben Ben is offline
Moderator
 
Join Date: Jul 2006
Posts: 830
Thanks: 5
Thanked 41 Times in 36 Posts
Default

@Falko: Really?

Because the loop runs always at least one time because the condition is at the bottom...

if it would be a
while( $row...)
it would be twice.

How about some var_dumps after fetching the data, or fater the call of distance, to manually check if there are any logical problems you did not see yet?
Reply With Quote
  #4  
Old 26th July 2006, 16:20
tagtekin tagtekin is offline
Junior Member
 
Join Date: Jul 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default ok here is the code i made some changes

ok here is the code i made some changes and also here is the link whati t gives me. I get some return results where

http://70.46.28.11/zip/tester.php?getzip=34744

zip code 34744 at least 400 miles from the miami location
and 34744 should be 0 miles from the 34744 location

thanks guys
<?php
//designed to calculate closest range 50 miles of offices for HIR
//two db tables and one database connections requred
$getzip = $_GET['getzip'];

$dbh=mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("phpziplocator");

$query1 = "Select * from zipdata where zipcode = '" . $getzip . "'";
$zip1 = mysql_query($query1) or die(mysql_error());
$row1 = mysql_fetch_assoc($zip1);


$query2 = "Select * from offices";
$zip2 = mysql_query($query2) or die(mysql_error());
$row2 = mysql_fetch_assoc($zip2);


do {
if (distance($row1['lon'], $row1['lat'], $row2['lon'], $row2['lat']) <= 100) {
echo $row2['city'] . " --- " . $row2['state'] . " --- " . $row2['zip'] . " --- <b>" . distance($row1['lon'], $row1['lat'], $row2['lon'], $row2['lat']) . "</b> --- " . $row2['lat'] . " --- " . $row2['lon'] . "<br>";



}
} while ($row2 = mysql_fetch_assoc($zip2));

//echo distance($row1['lat'], $row1['lon'], $row2['lat'], $row2['lon'], "M") . " Miles<br>";

mysql_free_result($zip1);
mysql_free_result($zip2);

function distance($lat1, $lon1, $lat2, $lon2)
{
$lat1 = floatval($lat1);
$lon1 = floatval($lon1);
$lat2 = floatval($lat2);
$lon2 = floatval($lon2);

// Formula for calculating distances
// from latitude and longitude.
$dist = acos(sin(deg2rad($lat1))
* sin(deg2rad($lat2))
+ cos(deg2rad($lat1))
* cos(deg2rad($lat2))
* cos(deg2rad($lon1 - $lon2)));

$dist = rad2deg($dist);
$miles = (float) $dist * 69;

// To get kilometers, multiply miles by 1.61
$km = (float) $miles * 1.61;

// This is all displaying functionality
$display = sprintf("%0.1f",$miles);

//$display = sprintf("%0.2f",$miles).' miles' ;
//$display .= ' ('.sprintf("%0.2f",$km).' kilometers)' ;

return $display ;
}
?>
Reply With Quote
  #5  
Old 26th July 2006, 16:44
Ben Ben is offline
Moderator
 
Join Date: Jul 2006
Posts: 830
Thanks: 5
Thanked 41 Times in 36 Posts
Default

What happens if you do the calculation for distance() manually with a calculator? So just echo the values you read from the db, an then
calculate or do what you except your script should do next.

If you finished correctly see, whats the difference in you manual process and in your code is.

I'm sorry that this answer is more theoretical but on the other hand the problem is "too complex" to just understand what this all shall do with any data from the db, and how to fix the "problem"...
Reply With Quote
  #6  
Old 26th July 2006, 16:49
tagtekin tagtekin is offline
Junior Member
 
Join Date: Jul 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default here is my logic

Yes even i did that the same zip codes should give me zero value. since both 34744 and 34744 located in the same coordinates. I am using the formula which i got from

$distance = sin(deg2rad($lat_A))
* sin(deg2rad($lat_B))
+ cos(deg2rad($lat_A))
* cos(deg2rad($lat_B))
* cos(deg2rad($long_A - $long_B));

$distance = (rad2deg(acos($distance))) * 69.09;

i also found similar link
http://www.dmxzone.com/forum/topic.asp?topic_id=14260
but still no good
thanks
Reply With Quote
  #7  
Old 26th July 2006, 19:29
tagtekin tagtekin is offline
Junior Member
 
Join Date: Jul 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Talking Guys I Got It

I got it i use different formula and i get very close milage results and here is my code anybody is more than welcome to use it and letm e know if there are questions about hte db and so on.

<?php
//designed to calculate closest range 50 miles of offices for HIR
//two db tables and one database connections required
$getzip = $_GET['getzip'];

$dbh=mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("phpziplocator");

$query1 = "Select * from zipdata where zipcode = '" . $getzip . "'";
$zip1 = mysql_query($query1) or die(mysql_error());
$row1 = mysql_fetch_assoc($zip1);


$query2 = "Select * from offices";
$zip2 = mysql_query($query2) or die(mysql_error());
$row2 = mysql_fetch_assoc($zip2);



$istartlat=81.3139;
$istartlong=28.66146;
$iRadius = 150;
$latrange = $iradius / ((6076 / 5280) * 60);
$LongRange = $iRadius / (((cos($iStartLat * 3.141592653589 / 180) * 6076.) / 5280.) * 60);

$LowLatitude = istartlat - $LatRange;
$HighLatitude = istartlat + $LatRange;
$LowLongitude = istartlong - $LongRange;
$HighLongitude = istartlong + $LongRange;

echo $HighLongitude;
echo("<br>");
echo $LowLongitude;
echo("<br>");
echo $HighLatitude;
echo("<br>");
echo $LowLatitude;
echo("<br>");

do {
if (distance($row1['lon'], $row1['lat'], $row2['lon'], $row2['lat']) <= 10000) {
echo $row2['city'] . " --- " .$row2['office_name'] . " --- " . $row2['state'] . " --- " . $row2['zip'] . " --- <b>" . distance($row1['lon'], $row1['lat'], $row2['lon'], $row2['lat']) . "</b> --- " . $row2['lat'] . " --- " . $row2['lon'] . "<br>";



}
} while ($row2 = mysql_fetch_assoc($zip2));

//echo distance($row1['lat'], $row1['lon'], $row2['lat'], $row2['lon'], "M") . " Miles<br>";

mysql_free_result($zip1);
mysql_free_result($zip2);

function distance($lat1, $lon1, $lat2, $lon2)
{
$lat1 = floatval($lat1);
$lon1 = floatval($lon1);
$lat2 = floatval($lat2);
$lon2 = floatval($lon2);

// Formula for calculating distances
// from latitude and longitude.

$dist = 3959*acos(sin(deg2rad($lat1/57.3))* sin(deg2rad($lat2)/57.3)+ cos(deg2rad($lat1)/57.3)* cos(deg2rad($lat2)/57.3)* cos(deg2rad($lon1 - $lon2)/57.3));

//$dist = rad2deg($dist);
$miles = (float) $dist * 69;

// To get kilometers, multiply miles by 1.61
$km = (float) $miles * 1.61;

// This is all displaying functionality
$display = sprintf("%0.1f",$miles);

//$display = sprintf("%0.2f",$miles).' miles' ;
//$display .= ' ('.sprintf("%0.2f",$km).' kilometers)' ;

return $display ;
}
?>
Reply With Quote
  #8  
Old 19th September 2006, 18:17
jokerjoe jokerjoe is offline
Junior Member
 
Join Date: Sep 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default I would like the DB structure

Could you please post the database structure for both databases. Thank you in advance.
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP & MySQL working, but AREN'T WORKING???? lipp9000 Installation/Configuration 4 21st July 2006 18:01
php script injections Grizzly General 21 18th July 2006 09:55
how to make php to work with mysql (php installed with apt-get, (no mysql support) grafik Installation/Configuration 3 16th May 2006 00:18
Downgrade php5 to php4.4.2 llizards Installation/Configuration 4 14th March 2006 00:58
Mandriva 10.2 Perfect Setup Install Problems... ctroyp Installation/Configuration 12 30th December 2005 17:04


All times are GMT +2. The time now is 02:54.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Sponsored Links: Unified Communications: Thoughts, Strategies and Predictions
Join the discussion.
www.seamlessenterprise.com

IP Convergence
Integrate your wireless and wireline networks.
Learn how from the experts at Sprint.
www.seamlessenterprise.com

Wireless & Wireline Integration
Thoughts, strategies and solutions: join the discussion
www.seamlessenterprise.com

Unified Communications 2009
Join the Discussion. Now.
www.seamlessenterprise.com

Red Hat Virtual Experience - a free virtual event. Dec. 9th