PHP Classes

File: ZIPLocator.php

Recommend this page to a friend!
  Classes of AzDG   AzDGZIP   ZIPLocator.php   Download  
File: ZIPLocator.php
Role: Class source
Content type: text/plain
Description: Class
Class: AzDGZIP
Lookup United States zip codes in a MySQL database
Author: By
Last change:
Date: 17 years ago
Size: 3,015 bytes
 

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 * 2 * 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;
        }

    }
}
?>