What is wrong with this php code?!?

Tom C

New member
I've got a csv spreadsheet with all the zip codes in california, and I want to be able to return results based on a zip code entered. In order to test it, I have already fixed exact coordinates for one location, and am trying to get the program to search the spreadsheet for the closest zip code.

For some reason the $distance variable always returns the same number! I've tested it and the program is definitely going through the spreadsheet, but for some reason that doesn't come through in the distance variable.

I'm pretty new at this, so any help would be greatly appreciated.




<?PHP

function calcDist($lat_A, $long_A, $lat_B, $long_B) {

$distance = sin(deg2rad($lat_A))
* sin(deg2rad($lat_B))
+ cos(deg2rad($lat_A))
* cos(deg2rad($lat_B))
* cos(deg2rad($long_A - $long_B));

$distance = (rad2deg(acos($distance))) * 69.09;


return $distance;
}

$file_handle = fopen("zip.csv", "r");

while (!feof($file_handle) )
{

$line_of_text = fgetcsv($file_handle, 1024);


$i=1;

//while ($i<=1672)
//{


$closestDistance =10000000000;

$zipDistance = calcDist(37.410932, -120.88972, $line_of_text[1], $line_of_text[2]);
echo $zipDistance;


if ($zipDistance<$closestDistance)
{
$nearestCity = $line_of_text[3];
}


//$i++;



//print $line_of_text[0] . $line_of_text[1]. $line_of_text[2] . "<BR>";

}

echo $nearestCity;

fclose($file_handle);


?>
 
Back
Top