PHP Classes
Icontem

File: ZIPLocator.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of AzDG  >  AzDGZIP  >  ZIPLocator.php  
File: ZIPLocator.php
Role: Class source
Content type: text/plain
Description: Class
Class: AzDGZIP
Lookup United States zip codes in a MySQL database
 

Contents

Class file image Download
<?php
############################################################
# \-\-\-\-\-\-\     AzDG  - S C R I P T S    /-/-/-/-/-/-/ #
############################################################
# Written by              AzDG (support@azdg.com)          #
# Created 23/04/07        Last Modified 23/04/07           #
# Scripts Home:           http://www.azdg.com              #
############################################################
# File name               ZIPLocator.php                   #
# File purpose            Class for ZIP distance count     #
# File created by         AzDG <support@azdg.com>          #
############################################################
# Idea from http://www.sanisoft.com "zipLocator" algorithm
# License LGPL
############################################################

class ZIPLocator
{
    var 
$Table;
    var 
$Round '2';
    function 
ZIPLocator($Table)
    {
        
$this->Table $Table;
    }
    
    function 
cb($ss)
    {
        
# Security on incoming data - never believe to anyone :)
        # Don`t check for intval(), because zip codes can be also a non-numeric!
        
return trim(htmlspecialchars(stripslashes($ss)));
    }

    function 
distance($zip1,$zip2)
    {
        
$lat $lon = array();
        
$tmp mysql_query('SELECT * FROM '.$this->Table.' WHERE zipcode IN ('.$this->cb($zip1).','.$this->cb($zip2).')');
        
$cnt mysql_num_rows($tmp);
        if(
$cnt 2)
        {
            return 
"One or both Zip Codes not found";
        }
        else
        {
            while(
$i mysql_fetch_array($tmp))
            {
                
$lat[] = $i['lat'];
                
$lon[] = $i['lon'];
            }

            
$lat[0] = $this->deg_to_rad($lat[0]);
            
$lon[0]  = $this->deg_to_rad($lon[0]);
            
$lat[1]  = $this->deg_to_rad($lat[1]);
            
$lon[1]  = $this->deg_to_rad($lon[1]);

            
$delta_lat $lat[1] - $lat[0];
            
$delta_lon $lon[1] - $lon[0];

            
$temp pow(sin($delta_lat/2.0),2) + cos($lat[0]) * cos($lat[1]) * pow(sin($delta_lon/2.0),2);

            
$EARTH_RADIUS 3956;
            
$distance $EARTH_RADIUS atan2(sqrt($temp),sqrt(1-$temp));

            return 
round($distance,$this->Round);
        }

    }

    function 
deg_to_rad($deg)
    {
        
$radians 0.0;
        
$radians $deg M_PI/180.0;
        return(
$radians);
    }


    function 
inradius($zip,$radius)
    {
        
$lat $lon '';$zips = array();
        
$tmp mysql_query('SELECT * FROM '.$this->Table.' WHERE zipcode = '.$this->cb($zip));
        
$cnt mysql_num_rows($tmp);
        if(!
$tmp)
        {
            return 
"Zip Code not found";
        }
        else
        {
            while(
$i mysql_fetch_array($tmp))
            {
                
$lat $i['lat'];
                
$lon $i['lon'];
            }
            
$tmp2 mysql_query('SELECT zipcode FROM '.$this->Table.' WHERE (POW((69.1*(lon-'.$this->cb($lon).')*cos('.$this->cb($lat).'/57.3)),2)+POW((69.1*(lat-'.$this->cb($lat).')),2))<('.$this->cb($radius).'*'.$this->cb($radius).')');
            
$cnt2 mysql_num_rows($tmp2);
            if(
$cnt2)
            {
                while(
$i mysql_fetch_array($tmp2))
                {
                    
$zips[]=$i['zipcode'];
                }
            }
            else
            {
                return 
"Zip Code not found";
            }
            return 
$zips;
        }

    }
}
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products