1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Zip Radius Help

Discussion in 'PHP' started by Sharpedgeflash, Dec 21, 2005.

Thread Status:
Not open for further replies.
  1. #1
    Hey all I got a problem. I am trying to figure out this Zip radius script that I have. Right now you can enter in a zip code and it will find all other zipcodes in a radius of 'X' miles and order them by distance. I am trying to figure out a way to have another query statement that checks to see if any of the ZIPcodes found exist in a table called users in a field called user_zip and if so it spits out those entries instead. Here is the code I have been using for the search. Any help with this is very much appreciated. Thanks

    <?php
    $db_Hostname = "localhost";
    $db_Database = "database";
    $db_UserName = "user";
    $db_Password = "pass";
    $set["db_table"] = "zip";

    if(!trim($_POST[zipcode])) $mess.="- zip code is required<br>";
    elseif(!is_numeric($_POST[zipcode])) $mess.="- zip code must be a number<br>";
    if(!trim($_POST[distance])) $mess.="- radius distance is required<br>";
    elseif(!is_numeric($_POST[distance])) $mess.="- radius distance must be a number<br>";

    mysql_connect($db_Hostname, $db_UserName, $db_Password) || die("Can't Connect to Database: ".mysql_error());
    mysql_select_db($db_Database);

    if(trim($mess)) {
    echo "<p>$mess</p>";
    } else {
    $z=RadiusSearch($_POST[zipcode],$_POST[distance],$_POST[unit]);
    $tot = count($z);
    $output = $z[$i]["zip"];
    if ($_POST[unit]== 0) { $_POST[unit] = "miles"; } else { $_POST[unit] = "KM"; }
    $array.="<table width='620' border='0' cellspacing='1' cellpadding='4' bgcolor='#CCCCCC' align='center'>";
    if(is_array($z) && $tot) {
    $array.="</tr>";
    for ($i=1;$i<count($z);$i++) {
    $array.="<tr bgColor='#FFFFFF'>
    <td>".$z[$i]["zip"]."</td>
    </tr>";
    }
    }
    echo $array."</table>";
    }

    function RadiusSearch($zipcode,$distance,$unit) // zip code, distance (KM), units(0 for miles, 1 for KM)
    {
    global $set;
    // This query statement finds the zip code that was entered
    $q=mysql_query("SELECT zip,Latitude,Longitude,sinlat,coslat,lonrad FROM $set[db_table] WHERE zip=$zipcode") or die(mysql_error());
    if(mysql_num_rows($q)) {
    $r=mysql_fetch_array($q);

    // This if statement checks to see if the unit is in miles or KM
    if ($unit==0) {
    //$cosd is equal to the appx. earth circumfrance times acos(sinlat x sinlat of the user zipcode + coslat x coslat of the user zipcode x the cos oflonrad - the lonrad of the user zipcode
    $cosd = "3963.1676*acos(sinlat*$r[sinlat]+coslat*$r[coslat]*cos(lonrad-$r[lonrad]))";
    } else {
    $cosd = "6378.1*acos(sinlat*$r[sinlat]+coslat*$r[coslat]*cos(lonrad-$r[lonrad]))";
    }

    // This query statement finds all other zip codes within the given radius
    $q=mysql_query("SELECT zip, city, county, state, latitude, longitude, ROUND($cosd, 1) AS dist FROM $set[db_table] WHERE $cosd <= $distance and zip<>$zipcode ORDER BY dist") or die(mysql_error());
    if(mysql_num_rows($q)) {
    // Looping array spits out all the zip codes with in the radius and orders them by distance
    while($r=mysql_fetch_row($q)) {
    $array[$i]["zip"] = $r[0];
    $array[$i]["distance"] = $r[6];
    $i++;
    }
    return $array;
    } else { echo "Sorry! No result found!"; exit; }
    } else { echo "Invalid zip code!"; exit; }
    }
    ?>
     
    Sharpedgeflash, Dec 21, 2005 IP
Thread Status:
Not open for further replies.