Thank you guys SO much for your help, my next delima is :
I want to make the "db" queries very generic so that anyone can use what ever db class or db functions or files they use for their queries and hook it in to this zipclass easily....LOL I think this may be more difficult?
I'll start a new thread call db zipcodes for anyone who wants to advise.
Seems like everytime I post a problem here my knowledge level goes up.
Is my coding style and documentation OK?
class zipLocator
{
/**
* Short description.
* This method returns the distance in Miles or Kilometers between two zip
* codes from primary zipcode table only.
*
* Detail description
* This method returns the distance in Miles between two zip codes,
* if either of the zip code is not found and error is retruned.
*
* @param zipOne - The first zip code
* @param zipTwo - The second zip code
* @param $units - The measurement mesthod either default miles or
* kilometers: "mi" or "km"
* @global db - the database object
* @since 1.0
* @access public
* @return string
* @update
* @output $distance = $zipLoc->distance($myZip,$zipTwo,$units);
*/
function distance($myZip,$zipTwo,$units)
{
global $db;
#-- Trim strip leading and trailing spaces
$myZip = trim($myZip);
$zipTwo = trim($zipTwo);
#myquery $query = db_res( "SELECT * FROM ZIPCodes WHERE ZIPCode = '$myZip' ");
$query = "SELECT * FROM ZIPCodes WHERE ZIPCode = '$myZip'";
$db->query($query);
#myquery $db->$query;
if(!$db->nf()) {
return "First Zip Code not found";
}else{
$db->next_record();
$lat1 = $db->f("Latitude");
$lon1 = $db->f("Longitude");
}
#myquery $query = db_arr( "SELECT * FROM ZIPCodes WHERE ZIPCode = '$zipTwo' ");
$query = "SELECT * FROM ZIPCodes WHERE ZIPCode = '$zipTwo'";
#myquery $db->$query;
$db->query($query);
if(!$db->nf()) {
return "Second Zip Code not found";
}else{
$db->next_record();
$lat2 = $db->f("Latitude");
$lon2 = $db->f("Longitude");
}
/* Convert all the degrees to radians */
$lat1 = $this->deg_to_rad($lat1);
$lon1 = $this->deg_to_rad($lon1);
$lat2 = $this->deg_to_rad($lat2);
$lon2 = $this->deg_to_rad($lon2);
/* Find the deltas */
$delta_lat = $lat2 - $lat1;
$delta_lon = $lon2 - $lon1;
/* Find the Great Circle distance */
$temp = pow(sin($delta_lat/2.0),2) + cos($lat1) * cos($lat2) * pow(sin($delta_lon/2.0),2);
$EARTH_RADIUS = 3956;
if($units== "km"){
$distance = round(1.61*($EARTH_RADIUS * 2 * atan2(sqrt($temp),sqrt(1-$temp))),1);
}else{
$distance = round($EARTH_RADIUS * 2 * atan2(sqrt($temp),sqrt(1-$temp)),1);
}
return $distance;
} // end func
/**
* Short description.
* Converts degrees to radians
* @param deg - degrees
* @global none
* @since 1.0
* @access private
* @return void
* @update
*/
function deg_to_rad($deg)
{
$radians = 0.0;
$radians = $deg * M_PI/180.0;
return($radians);
}
/**
* Short description.
* This method retruns an array of ZIPCodes found with the radius supplied
* from Primary Zipcode.
*
* Detail description
* This method returns an array of ZIPCodes found with the radius supplied
* in miles, if the zip code is invalid an error string is returned.
*
* @param $zip - The zip code: valid zipcode
* @param $radius - The radius from $zip(VERTEX) in miles or Kilometers: INT
* @param $sort - The Sort order either ASCEND or DESCEND: "ASC", "DESC" or ""
* @param $units - The measurement mesthod either default miles or kilometers: "mi" or "km"
* @global db - instance of database object
* @since 1.0
* @access public
* @return array/string
* @update
* @output $zipArray = $zipLoc->inradius($myZip,$radius,$units,$sort);
*/
function inradius($myZip,$radius,$units,$sort)
{
global $db;
#-- Trim strip leading and trailing spaces
$myZip=trim($myZip);
if($units=="km")
$km=1.61."*";
#myquery $query = db_arr( "SELECT * FROM ZIPCodes WHERE ZIPCode = '$myZip' ");
#myquery $db->$query;
$query="SELECT * FROM ZIPCodes WHERE ZIPCode='$myZip'";
$db->query($query);
if($db->affected_rows()<>0) {
$db->next_record();
$lat=$db->f("Latitude");
$lon=$db->f("Longitude");
$query="SELECT ZIPCode, ($km(POW((69.172*(Longitude-\"$lon\")*cos($lat/57.2958)),\"2\")+POW((69.1*(Latitude-\"$lat\")),\"2\"))) AS distance FROM ZIPCodes WHERE ($km(POW((69.172*(Longitude-\"$lon\")*cos($lat/57.2958)),\"2\")+POW((69.1*(Latitude-\"$lat\")),\"2\")))<= ($radius*$radius) ORDER BY distance $sort";
$db->query($query);
#myquery $db->$query;
if($db->affected_rows()<>0) {
#myquery if($db->@mysql_affected_rows($db)<>0) { Attemped to replace what they are doing here, didn't work
$i=0;
while($db->next_record()) {
$zipArray[$i]=$db->f("ZIPCode");
$i++;
}
}
}else{
return "Zip Code not found";
}
return $zipArray;
} // end func
} // end class