Alright… in my pursuit to convert my current site from Coldfusion to PHP, I ran into a problem with making CFC functions into PHP functions. Pretty much what I’m doing is recreating a proximity search. I think I’ve almost got it but I don’t know how PHP handles arrays and how to call to them. Here is the code I have…
<?
//constant variables
//pi divided by 180
$piDivRad = 0.0174;
//number of miles per degree of latitude
$latitudeMiles = 69.1;
//radius of the earth in miles
$earthRadius = 3956;
//makes an array with coordinates reset to zero
function newCoordinates() {
$newCoor = array();
$newCoor['latitude'] = 0;
$newCoor['longitude'] = 0;
$newCoor['rlatitude'] = 0;
$newCoor['rlongitude'] = 0;
}
//finds the coordinates from the list in the database by using the zipcode given
function zipCoordinates($zip) {
$zipCoor = newCorordinates();
$query = "SELECT latitude, longitude, rlatitude, rlongitude FROM zipcodes WHERE zip = '$zip'";
$result = mysql_query($query);
if ($zipcode = mysql_fetch_array($result)) {
$zipCoor['latitude'] = $zipcode['latitude'];
$zipCoor['longitude'] = $zipcode['longitude'];
$zipCoor['rlatitude'] = $zipcode['rlatitude'];
$zipCoor['rlongitude'] = $zipcode['rlongitude'];
}
//return the coordinates for the lat and long
return $zipCoor;
}
function squareSearch($radius, $zip) {
$radius = $radius; //maybe not needed?
$zip = $zip; //maybe not needed?
$zipCoor = zipCoordinates($zip);
$formula = $earthRadius * (ACOS((SIN($zipCoor['latitude']/57.2958) * SIN(latitude/57.2958)) + (COS($zipCoor['latitude']/57.2958) * COS(latitude/57.2958) * COS(longitude/57.2958 - $zipCoor['longitude']/57.2958))))
$query = "SELECT zip, latitude, longitude, '$formula' FROM zipcodes WHERE '$formula' <= '$radius'";
$result = mysql_query($query);
//return the results
return $result;
}
?>
Alright… for the function zipCoordinates I have a variable ($zipCoor) calling the function newCorordinates().
$zipCoor = newCorordinates();
Because the function creates an array, will the variable $zipCoor become an array?
$zipCoor = [0, 0, 0, 0]?
Also is my if statement correct in zipCoordinates()?
Last, since squareSearch() will return multiple results will I need to make a while statement? or is the return $result at the end of the function sufficient?