i'm attempting to get a 'dealer' locator working based on a user entered zip code and distance constraint.

I have two tables, zipdata (which has zip data) and epdb (which has dealer name, addrss, zip, etc).

Right now my query seems to be grabbing the first lat/long in the zipdata table and using that, rather than finding the correct lat/long for each dealer, and continuing the calculations. I'm a bit confused on how to acheive this.

If anyone could give me some hints/guide me a bit it'd be very much appreciated.

I've based a lot of this on a post I found while searching this forum: http://www.phpbuilder.com/board/showthread.php?threadid=10290629&highlight=zip+code+locator which has a lot of useful information, but I need additional help.

<?php

include 'dbconnect.php';

$process = $_SERVER['PHP_SELF'];

if(!isset($_POST['submit'])) {

echo ("
	<form action='$process' method='post' name='enterZip'>
	<table border=0>
	<tr>
	<td colspan='2'>Enter Zip Code and select mileage constraint.</td>
	</tr>
	<tr>
	<td align='right'>Zip Code:</td>
	<td><input type='text' name='ZipCode1' value='95641' size=4 maxlength=5></td>
	</tr>
	<tr>
	<td align='right'>Mileage:</td>
	<td><select name='mileage'>
	<option value='25' selected>25 miles</option>
	<option value='50'>50 miles</option>
	<option value='75'>75 miles</option>
	<option value='100'>100 miles</option>
	<option value='10000' selected>more than 100 miles</option>
	</select></td>
	</tr>
	<tr>
	<td>&nbsp;</td>
	<td><input type='submit' name='submit' value='Submit'></td>
	</tr>
	</table>
	");
} 

else { 

// retreive from zipdata table the latitude/longitude for provided zip code (ZipCode1)

$ZipCodeSupplied1 = $_POST['ZipCode1'];

$query1 = ("
		SELECT	ZipDataID,
		ZipCode, 
		Latitude,
		Longitude		

	FROM zipdata

	WHERE ZipCode LIKE $ZipCodeSupplied1;	
");

$resultQuery1 = mysql_query($query1) or die (mysql_error());

if ($row = mysql_fetch_array($resultQuery1)) {

$Latitude = $row["Latitude"];
$Longitude = $row["Longitude"];   
$mileage = $_POST['mileage'];

echo ("<p>Query Information:<br>Zip Code: $ZipCodeSupplied1<br>Lat: $Latitude, Long: $Longitude<br>Mileage Constraint: $mileage"); // query 1 info

$LatitudeR = deg2rad($Latitude);
$LongitudeR = deg2rad($Longitude);

// echo ("<p>Radian Lat: $LatitudeR and Long: $LongitudeR"); // test only - for radian values

}

$StZipCode = substr($ZipCodeSupplied1,0,3);

$query2 = ("
		SELECT	epdb.CN,
		epdb.epdbID,
		epdb.Branch,
		epdb.CompanyName,
		epdb.FirstName,
		epdb.LastName,
		epdb.Email,
		epdb.Address,
		epdb.City,
		epdb.State,
		epdb.Zip,
		epdb.AreaPhone,
		epdb.Phone,
		epdb.Phone1,
		epdb.AreaFax,
		epdb.Fax,  
epdb.Fax1, zipdata.Latitude AS Latitude, zipdata.Longitude AS Longitude, ((ACOS((SIN($LatitudeR) * SIN(Latitude/57.2958)) + (COS($LatitudeR) * COS(Latitude /57.2958) * COS(Longitude/57.2958 - $LongitudeR)))) * 3963) AS Distance FROM zipdata, epdb WHERE epdb.Zip LIKE '$StZipCode%' AND ((ACOS((SIN($LatitudeR) * SIN(Latitude /57.2958)) + (COS($LatitudeR) * COS(Latitude /57.2958) * COS(Longitude/57.2958 - $LongitudeR)))) * 3963) < $mileage GROUP BY epdb.CN ORDER BY Distance ASC LIMIT 5; "); $resultQuery2 = mysql_query($query2) or die (mysql_error()); echo (" <table border=0> <tr> <td><hr></td> </tr> "); while ($row = mysql_fetch_array($resultQuery2)) { $pID = $row["epdbID"]; $Branch = $row["Branch"]; $CN = $row["CN"]; $CompanyName = $row["CompanyName"]; $FirstName = $row["FirstName"]; $LastName = $row["LastName"]; $Email = $row["Email"]; $Address = $row["Address"]; $City = $row["City"]; $State = $row["State"]; $ZipCode = $row["Zip"]; $AreaPhone = $row["AreaPhone"]; $Phone = $row["Phone"]; $Phone1 = $row["Phone1"]; $AreaFax = $row["AreaFax"]; $Fax = $row["Fax"]; $Fax1 = $row["Fax1"]; $Distance = $row["Distance"]; $Latitude2 = $row["Latitude"]; // for testing $Longitude2 = $row["Longitude"]; // for testing $DistanceR = substr($Distance,0,4); echo (" <tr> <td><b>$CompanyName (#$CN)</b> <br>$FirstName $LastName <br>$Address, $City, $State $ZipCode ($StZipCode) <br>($AreaPhone) $Phone-$Phone1 PHONE, ($AreaFax) $Fax-$Fax1 FAX <br><a href='mailto:$Email'>$Email</a> E-mail <br>Mileage: $DistanceR (Lat: $Latitude2, Long: $Longitude2 )</td> </tr> <tr> <td><hr></td> </tr> "); } echo (" </table> "); } ?>

    Here's a script I have for a site I am writing. (I haven't incorporated the category portion of the search into it yet, but you can see what it's doing... Basically, I do a similar query (with a slightly different Great Circle equation) and when I display the results, I then query the company table to get that particular company's info. Keep in mind, this script is not fully functional yet, but the results portion is working fine. It queries over 40,000 rows in about 1 second locally and about 2.5-3 seconds online (56k). I know my response seems a bit lazy, but honestly, I don't feel like picking through your code, so I figured since you have the question, you can (and should be able to pretty easily) pick through my code instead if you so desire... Let me know if something I have done doesn't make sense to you or you need any other help after trying to decipher my code. 🙂

    <?php
    if(file_exists('./includes/config.inc.php')){
    	include('./includes/config.inc.php');
    }
    if(file_exists('./includes/functions.inc.php')){
    	include('./includes/functions.inc.php');
    }
    ?>
    <html>
    <head>
    <title>Coming Soon:  Surf.Print.Shop Your way to Savings!</title>
    <link rel="stylesheet" type="text/css" href="./mainstyle.css">
    </head>
    <body style="background-color: #FFFFFF;">
    
    <div style="text-align:center; font-size: 12px; font-family: verdana;">
    <i style="font-size: 11px; font-weight: bold;">Coming Soon...</i><br />
    <img src="./images/surfprintshopheader.png" width="700" height="87"><br />
    <?php
    $connection = mysql_connect($host,$user,$password);
    mysql_select_db($db,$connection);
    ?>
    <table width="700" border="0" cellspacing="3" cellpadding="3">
    <tr>
    	<td width="200" valign="top">
    <form action="<?php echo $thispage; ?>" method="post">
    Surf for Savings<br /><br />
    <select class="forms" name="categories">
    <option value="0" />All Categories
    <option value="0" />--------------------
    <?php
    $getCategories = mysql_query("SELECT * FROM categories WHERE Parent_Category_ID = '0' ORDER BY Category_Name ASC");
    while($gotCategories = mysql_fetch_array($getCategories)){
    	echo "<option value=\"".$gotCategories['Category_ID']."\" />".$gotCategories['Category_Name'];
    }
    ?>
    </select><br />
    Zip Code:&nbsp;&nbsp;<input class="forms" type="text" name="userZip" size="5" value="<? if(isset($_POST['userZip'])) echo $_POST['userZip'] ?>"><br />
    Distance:&nbsp;&nbsp;
    <select class="forms" name="radius">
    <option value="5" />5
    <option value="10" />10
    <option value="25" />25
    </select><br />
    <input class="buttonsubmit" type="submit" name="doItNow" value="Search">
    </form>
    		</td>
    		<td>
    <?php
    if(!empty($_POST['userZip']) && !empty($_POST['radius'])){
    	$userZip = $_POST['userZip'];
    	$userRadius = $_POST['radius'];
    
    $getuserPoints = mysql_query("SELECT * FROM region WHERE Region_Zip = '".$userZip."'");
    $gotuserPoints = mysql_fetch_array($getuserPoints);
    
    $userLat = $gotuserPoints['Region_Latitude'];
    $userLong = $gotuserPoints['Region_Longitude'];
    $userLocation = $gotuserPoints['Region_City']." - ".$gotuserPoints['Region_ST'];
    
    $regionSQL = "SELECT";
    $regionSQL .= " *, SQRT((69.1*(".$userLat."-r.Region_Latitude))*(69.1*(".$userLat."-r.Region_Latitude))+(53.0*(".$userLong."-r.Region_Longitude))*(53.0 *(".$userLong."-r.Region_Longitude))) AS MyDistance";
    $regionSQL .= " FROM region AS r";
    $regionSQL .= " WHERE (SQRT((69.1*(".$userLat."-r.Region_Latitude))*(69.1*(".$userLat."-r.Region_Latitude))+(53.0*(".$userLong."-r.Region_Longitude))*(53.0 *(".$userLong."-r.Region_Longitude))) <= ".$userRadius." )";
    $regionSQL .= " ORDER BY MyDistance, r.Region_City";
    $getRegions = mysql_query($regionSQL);
    $countem = mysql_num_rows($getRegions);
    
    echo $countem."<br />";
    while($location = mysql_fetch_array($getRegions)){
    	echo "<fieldset><legend>Location within ".$userRadius." miles from ".$userLocation."</legend>\n";
    	echo "City: ".$location['Region_City']."<br>State: ".$location['Region_ST']."<br>zipcode: ".$location['Region_Zip']."<br>Approximate Distance: ".round($location['MyDistance'],1)." miles";
    	echo "</fieldset>";
    }
    } else {
    	$getCatsQuery = "SELECT * FROM categories WHERE Parent_Category_ID = '0' ORDER BY Category_Name";
    	$getCats = mysql_query($getCatsQuery);
    	while($gotCats = mysql_fetch_array($getCats)){
    		$getCouponQuery = "SELECT count(Coupon_ID) AS countcoup FROM coupons WHERE Category_ID = '".$gotCats['Category_ID']."'";
    		$getCoupCount = mysql_query($getCouponQuery);
    		$gotCoupCount = mysql_fetch_array($getCoupCount);
    
    	echo $gotCats['Category_Name'];
    	if($gotCoupCount['countcoup'] != 0){
    		echo " - ".$gotCoupCount['countcoup']."<br />";
    	} else {
    		echo "<br />";
    	}
    }
    }
    ?>
    		</td>
    	</tr>
    </table>
    </body>
    </html>
    

      I'd say start by troubleshooting your distance calculating query. Dump it out to the browser and if you're able to, paste it here so we can take a look at it. Then run the query in phpmyadmin. Do you only get one result? If so, you definitely have a query problem. You may find tweaking the query in phpmyadmin a lot easier than doing it in the php page.

        Write a Reply...