SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: zip code radius

  1. #1
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    zip code radius

    I am using the following function from phpclasses.org.

    How can I gues this to return also the distance for each zip code?

    PHP Code:
      function inradius($zip,$radius)
        {

            
    $query2="SELECT * FROM zips WHERE zip='$zip'";
            
    $row $db->get($query2);

                
    $lat=$row["lat"];
                
    $lon=$row["lon"];
                
    $query2="SELECT zipcode FROM zips WHERE (POW((69.1*(lon-\"$lon\")*cos($lat/57.3)),\"2\")+POW((69.1*(lat-\"$lat\")),\"2\"))<($radius*$radius) ";
                
    $result $database->query($query2);
                if(
    $result->count()<>0) {
                    while(
    $row $result->fetch()) {
                        
    $zips[$i]=$row["zipcode"];
                        
    $i++;
                    }
                }
            }else{
                return array();
            }
         return 
    $zips;
        } 

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
                $query2="SELECT zipcode, (POW((69.1*(lon-\"$lon\")*cos($lat/57.3)),\"2\")+POW((69.1*(lat-\"$lat\")),\"2\")) AS `distance` FROM zips WHERE `distance` <($radius*$radius) ";
                
    $result $database->query($query2);
                if(
    $result->count()<>0) {
                    while(
    $row $result->fetch()) {
                        
    $zips[$i]['zip'] = $row["zipcode"];
                        
    $zips[$i]['distance'] = $row["distance"];
                        
    $i++;
                    }
                } 
    Not sure why you have double quotes around half the numbers, and you can definitely do this whole thing in one query instead of two, but I'm going to sleep now.
    Last edited by Dan Grossman; May 15, 2007 at 00:08.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •