All pastes #581260 Raw Edit

Something

public text v1 · immutable
·published 2007-06-21 13:02 UTC
rendered paste body
<?php
show_source( __FILE__ );

function getinfo($zip) {
include('dbconnect.php');
$sql = "SELECT * FROM areas WHERE zip = '$zip'";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$all['state'] = $row['state'];
$all['county'] = $row['county'];
$all['city'] = $row['city'];
$all['zip'] = $row['zip'];
$all['latz'] = $row['latz'];
$all['longz'] = $row['longz'];
$all['population'] = $row['population'];
$all['avgincome'] = $row['avgincome'];
 $sql = "SELECT x1.zip,x1.city,x1.state,x1.city, x1.county, x1.latz, x1.longz  FROM
                areas x1, areas x2 WHERE
                    3963.191 * ACOS( (SIN(PI()* x2.latz /180)*SIN(PI() * x1.latz/180)) +
                        (COS(PI()* x2.latz /180)*cos(PI()*x1.latz/180)*COS(PI() * x1.longz/180-PI()* x2.longz /180))
                        ) <= " . intval( $miles ) . " AND x2.zip = " . intval( $zip );

    $result = mysql_query( $sql ) or die(mysql_error());
    $myresult = array();
    $x = 0;
    while( $row = mysql_fetch_array( $result ) )
    {
        $myresult[$x]['zip'] = $row['zip'];
        $myresult[$x]['city'] = $row['city'];
        $myresult[$x]['state'] = $row['state'];
        $myresult[$x]['county'] = $row['county'];
        $myresult[$x]['longz'] = $row['longz'];
        $myresult[$x]['latz'] = $row['latz'];
        $x++;
    }

    return $myresult;
}

function getDistance($lat1, $long1,  $lat2, $long2)
{
//$earth = 6371; //km change accordingly
$earth = 3960; //miles

//Point 1 cords
$lat1 = deg2rad($lat1);
$long1= deg2rad($long1);

//Point 2 cords
$lat2 = deg2rad($lat2);
$long2= deg2rad($long2);

//Haversine Formula
$dlong=$long2-$long1;
$dlat=$lat2-$lat1;

$sinlat=sin($dlat/2);
$sinlong=sin($dlong/2);

$a=($sinlat*$sinlat)+cos($lat1)*cos($lat2)*($sinlong*$sinlong);

$c=2*asin(min(1,sqrt($a)));

$d=round($earth*$c);

return $d;
}


$zip = $_GET['zip']; if(!isset($_GET['zip'])) { $zip = 10604; }
$miles =  $_GET['miles'] ;    if(!isset($_GET['miles'])) { $miles = 5; }
    $codes = xmileradius( $zip, $miles );
    $info = getinfo($zip);

echo "These codes are within " . $miles . " miles from " . $info['city']  . "<br />";

//var_dump($codes);
for($x=0;$x < count($codes); $x++ )
{
    echo $codes[$x]['zip'] . " " . $codes[$x]['city'] . ", " . $codes[$x]['state'] . " " . getDistance($codes[$x]['latz'],$codes[$x]['longz'],$info['latz'],$info['longz']) . " miles<br />";
}
?>