Jump to content

SORT results of SQL or POPULATE Multidimensional Array & Sort


cjbeck71081

Recommended Posts

I have a zip code locator that i am writing script for and ive come across a problem.  I have found a way to cross reference the LAT & LONG from a Zip Code table i have in my SQL datbase.  I have another table that has the 25+ locations with thier LAT & LONG. I have managed to take the data from each of those tables and calculate distance with them, pull down specific information about the locations within a certain range of the users ZIP code.  The problem i have is i need to be able to sort the information numerically.  I thought the best way to do that is to populate a multidimensional array, then sort the array and "echo" the values of the array, wherever i needed them.  However if there is a better way, im open to new ideas.

 

Thanks in advance, im posting my code below.

 

<?

$zip = $_POST['zip'];
$dis = $_POST['dis'];


$con = mysql_connect($hostname,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db($dbName, $con);

$query = "SELECT * FROM zip_code WHERE zip_code = '$zip'";

$result = mysql_query($query)or die ("Query Failed: $query - " . mysql_error());


$row = mysql_fetch_array($result);

$lat1 = $row['lattitude'];
$lon1 = $row['longitude'];


$locquery = "SELECT * FROM locations";
$locresult = mysql_query($locquery)or die ("Query Failed: $query - " . mysql_error());

$myarray = array();

while($locrow = mysql_fetch_assoc($locresult)){


$lat2 = $locrow['loclat'];
$lon2 = $locrow['loclon'];
$myzip = $locrow['loczip'];
$myadd	= $locrow['locadd'];
$myid = $locrow['locid'];

$dist   = acos(sin(deg2rad($lat1))
              * sin(deg2rad($lat2))
              + cos(deg2rad($lat1))
              * cos(deg2rad($lat2))
              * cos(deg2rad($lon1 - $lon2)));

    $dist   = rad2deg($dist);
    $miles  = (float) $dist * 60;


if($miles < $dis){

$thisarray = ($miles = $myzip);

array_push($myarray, $thisarray);
echo($myadd." is ".$miles." from ".$zip."<br>");
}
}

?>

 

As you can probably see, the problem im having is that the array_push is not putting my values in properly.  Anyway, any help is appreciated.

 

Thanks

Chris

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.