Jump to content

Pleasehelp me understand this code


2levelsabove

Recommended Posts

What is $dif ???????????

 

 

And why is "$query = "select * from zips where zips.lat < $lat1 AND zips.lat > $lat2 AND zips.lon < $lon1 AND zips.lon > $lon2";"

 

 

used ?

 

 

Please explain.

 

Thank you!!

 

 


@$zipsearch = $_GET['zipsearch'];
@$radius = $_GET['radius'];
$dif = '2';
$results='';
$data=array();

//if(!empty($zipsearch)){}

$query = "select * from zips where zip = '$zipsearch' LIMIT 1";
$result = mysql_query($query);
$row = mysql_fetch_assoc($result);

if(empty($row)){$results = "<tr><td align='center' class='results' style='font-size:16px;font-weight:bold;color:red;font-family:arial;'>Invalid zip code</td></tr>";}

$lat = $row['lat'];$lon = $row['lon'];

$lat1 = ($lat + $dif);
$lat2 = ($lat - $dif);
$lon1 = ($lon + $dif);
$lon2 = ($lon - $dif);

$query = "select * from zips where zips.lat < $lat1 AND zips.lat > $lat2 AND zips.lon < $lon1 AND zips.lon > $lon2";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
        $dist = great_circle_distance($lat,$row['lat'],$lon,$row['lon']);
        if ($dist < $radius){
        array_unshift($row,$dist);
               $row[0]=round($row[0],2);
               $data[] = $row;
        }
}

sort($data); // $data now has the zip codes and distances in increasing order of distance from $_GET['zipsearch'];

function great_circle_distance($lat1,$lat2,$lon1,$lon2){
               /* Convert all the degrees to radians */
               $lat1 = deg_to_rad($lat1);
               $lon1 = deg_to_rad($lon1);
               $lat2 = deg_to_rad($lat2);
               $lon2 = 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;

               $distance = $EARTH_RADIUS * 2 * atan2(sqrt($temp),sqrt(1-$temp));

               return $distance;
}

function deg_to_rad($deg){
               $radians = 0.0;
       
               $radians = $deg * M_PI/180.0;

               return($radians);
}

Link to comment
https://forums.phpfreaks.com/topic/101394-pleasehelp-me-understand-this-code/
Share on other sites

Its actually not very good programming. Programmers put it in to suppress errors, but if the code was written properly in the first place, the errors wouldn't need to be suppressed. Error handling should be handled by writing code to handle it, not by suppressing it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.