Geocoding UK Postcodes with Google Map API

What I want to do - I have a few schools in a database (three to be exact, just to get me started) each with their own latitude and longitude. When a User enters in their postcode which in turn produces their latitude and longitude, I want the page to come back telling them their nearest school and their distance away from it.

I followed a following tutorial which basically tells a user what their longitude and latitude is.
Original source: http://www.tomanthony.co.uk/blog/geocoding-uk-postcodes-with-google-map-api/

So I have the following:

  • Gmap.js - A JavaScript file (that is pulled in by the HTML page).
    Original source: http://www.tomanthony.co.uk/demo/geocode_uk_postcode/gmap.js

  • Main.html - An HTML page that pulls in Gmap.gs in order to calculate a Users longitude and latitude depending on the postcode that they have entered into a text box and puts the longitude and latitude into their respective textfields.

In order to calculate the distance between two points, I edited some PHP code to calculate the distance between two points given the latitude/longitude of those points. I’m at a very early stage with this code – it doesn’t do a great deal at the moment other than produce a distance in miles.

Original source: http://www.zipcodeworld.com/samples/distance.php.txt

Distance.php

[php]<?php

/::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::/
/:: :/
/:: this routine calculates the distance between two points (given the :/
/:: latitude/longitude of those points). it is being used to calculate :/
/:: the distance between two zip codes or postal codes using our :/
/:: zipcodeworld™ and postalcodeworld™ products. :/
/:: :/
/:: definitions: :/
/:: south latitudes are negative, east longitudes are positive :/
/:: :/
/:: passed to function: :/
/:: lat1, lon1 = latitude and longitude of point 1 (in decimal degrees) :/
/:: lat2, lon2 = latitude and longitude of point 2 (in decimal degrees) :/
/:: unit = the unit you desire for results :/
/:: where: ‘m’ is statute miles :/
/:: ‘k’ is kilometers (default) :/
/:: ‘n’ is nautical miles :/
/:: united states zip code/ canadian postal code databases with latitude & :/
/:: longitude are available at http://www.zipcodeworld.com :/
/:: :/
/:: For enquiries, please contact [email protected] :/
/:: :/
/:: official web site: http://www.zipcodeworld.com :/
/:: :/
/:: hexa software development center © all rights reserved 2004 :/
/:: :/
/::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::/
function distance($lat1, $lon1, $lat2, $lon2, $unit) {

$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;

    return $miles;

}
echo distance(32.9697, -96.80322, 29.46786, -98.53506, “m”) . " miles
";

?>
[/php]
Can you advise me as to what to do next with this PHP file in order to do as per the first paragraph of my post? I’m not sure how to break down what I need to do, where to do what, and what to do first.

Thanks, and if there are any questions just let me know.

Well, a postal code here in the states can give you an exact location. Therefore, should give an exact Lat/Long.
Then, I assume you just find the closest school by checking them all with a loop that would find the smallest of the two location variables. This would give you the correct school.

To accomplish this task, you would have to load the table of schools and location into a table and loop thru it.
Keep the first school as a match, noting the location. Then, move on to the next one. If it has a closer location,
then move it to the matched spot and discard the previous match. Once you at the end of the loop, you have your school closest to the location of the user.

Of course, this is if you have your figures correct on converting the Lat/Long to miles. If so, this does not sound like a very hard project. Do you have any PHP code questions about how to handle it? That’s what we are here for… Good luck, sounds like an interesting project.

Just for clarity, I am using UK-based postcodes. Yes that’s right, what I have passes the latitude and longitude into two text boxes following a User’s entry of their postcode and the click of a button.

Since my last message changed the following in distance.php:

[php]echo distance(32.9697, -96.80322, 29.46786, -98.53506, “m”) . " miles
";[/php]
to:
[php]echo distance($lat1, $lon1, $lat2, $lon2, $unit, “m”) . " miles
";[/php]

I then renamed ‘Main.html’ to ‘Main.php’ and added the following towards the end:

[php]<?php
include “distance.php”;
distance($lat1, $lon1, $lat2, $lon2, $unit);
?>[/php]
I have done all of this is, so as to retrieve the latitude and longitude and display the number of miles.
But feel free to correct any of the above if I am on the wrong lines.

Then, I assume you just find the closest school by checking them all with a loop that would find the smallest of the two location variables. This would give you the correct school.

PHP Code questions: For loop, foreach loop? What needs to go into the loop for this? How do I handle it?

I have checked this using distance.php. I included the code in my last post…

If it helps, you can do the same by putting in two latitude and longitude values of your choice if and checking the number of miles it displays when you run it to see if it is correct.

That’s good - I may be over analysing in what I thought I needed but that’s why I decided to post here for help. Hopefully this will help me to make progress on it.

Thanks.

Sounds like you are getting close in your project.

Well, FOR loops are easy. Here is the PHP notes on how to use them:
http://php.net/manual/en/control-structures.for.php

For your project, first you need a list of all of the schools and their Lat/Lon values.
They must be put into a table or database so that you can loop thru them with your FOR code.

Let’s say the table for your schools looks something like this: (just an basic idea)
School-Number School-Postal-Code School-Lat School-Lon
1 ??? 32.9697 -96.80322
2 ??? 32.9633 -95.44322
3 ??? 32.3397 -96.76522

This type of data (which I made up) would be loaded from your database and placed into
an php array. Your FOR loop would loop thru the array doing the math needed to locate the
miles value. Inside the loop, you would keep track of just the lowest miles and which school
number it was. When the loop finishes, you know the school-number of the lowest distance.

Another way to do this would be to query your database with something like “SELECT * FROM Schools”
and select all of the data rows for all schools. Then, inside the FOR loop pull the Lat/Lon fields for each
school record and do the math. I like this way best. It means you do not have to deal with arrays.

In general (NOT CODE), her is how you would do this:
Set up a connection to your database
$ClosestSchoolMiles=99999; Create a variable to save miles of closest school
$ClosestSchoolNumber=0; actual number of row of data pointing to closest school
$result = mysql_query(“SELECT School-Number, School-Lat, School-Lon FROM School-Table”);
(This query’s your data and gets what we need for data, next loop thru the rows of data)
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
$SchoolNumber = $row[0];
$SchoolLat = $row[1];
$SchoolLon = $row[2];
$Miles = distance($UserLat, $UserLon, $SchoolLat, $SchoolLon, “m”);
if ($Miles < $ClosestSchoolMiles) {
$ClosestSchoolMiles = $Miles;
$ClosestSchoolNumber = $SchoolNumber;
}
}
At the end of this loop, variable $ClosestSchoolNumber will equal the best scholl and the
variable $ClosestSchoolMiles will contain the miles to that school. This was just an example. Of course change the names and fix up the code correctly. It was just a layout of what you need.

Hope that helps…

Thank you for your input ErnieAlex, but I am finding your post very hard to follow.

I see you are just elaborating on my first post with regard at was said on the database. This is fine if it helps you.

$Miles = distance($UserLat, $UserLon, $SchoolLat, $SchoolLon, "m"); if ($Miles < $ClosestSchoolMiles) { $ClosestSchoolMiles = $Miles; $ClosestSchoolNumber = $SchoolNumber;

The use of $Miles (with a capital ‘M’) I assume indicates that this a new variable and is different to the $miles variable that you would have seen in my code? I’m confused by this, don’t understand the logic here. Can you please explain?

I am unsure of the description of the; for loop which is making me think that it needs to have a foreach construct instead because it will allow me to count the number of records in the database as it loops through it (I think).

However I have added some coding to distance.php to get me started.

[php]// Create page variables
$p = NULL;
$locations= NULL;

$con=mysqli_connect(‘localhost’, ‘username’, ‘password’);
mysqli_select_db($con, ‘username’);

// Declare page functions
function distance($lat1, $lon1, $lat2, $lon2, $unit) {

$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;

    return $miles;

}

if (isset ($_POST[‘submitted’])) {

$query = “SELECT lat, lon FROM postcodes WHERE postcode = ‘$p’”;

$result=mysqli_query($con, $query) or die ('Invalid query.');

// Retrieve coordinates of the locations
$locations= array();
$query = “SELECT schoolname, address, district, postal, lat, lon
FROM locations
INNER JOIN postcodes
ON locations.postal = postcodes.postcode”;
$result = mysqli_query ($con, $query);

// Go through and check all locations
while ($row = mysqli_fetch_assoc ($result)) {

  // Separate closest locations
  $distance = Dist ($row['lat1'], $row['lon1'], $postcode['lat2'], $postcode['lon2']);

    $locations[] = array (
      'schoolname'     => $row['schoolname'],
      'address'   => $row['address'],
      'district'  => $row['district'],
      'postal'    => $row['postal'],
    );

//output miles
echo distance($lat1, $lon1, $lat2, $lon2, “m”) . " miles
";
}[/php]

I’m a bit dissapointed by lack of progress at this stage, but if anyone would like to correct anything I have done or direct and advise me as to what I should do next, please do, as I need the help. Thanks.

Replying to myself here - I know, I know!! More or less what I thought as this is proving to be fairly difficult as you can probably see so literally any help you can give is much appeaciated and don’t be put off by a
previous post.

Decided to start again, as I couldn’t get it working. A backwards step-maybe, but things were getting a little too quiet, and time waits for no man!

Print the location_id latitude and longitude from table of postcodes.

[php]$con=mysqli_connect(‘localhost’, ‘username’, ‘password’) or die (‘No connection’);

mysqli_select_db($con, 'username' ) or die ('test will not open');

    $query = "SELECT location_id, lat, lon FROM `mylocations`";

$result = mysqli_query($con, $query) or die("Invalid query");

echo "<table border='1'><tr><th>Location_ID</th><th>Lat</th>";
echo "<th>Lon</th></tr>";	

while ($row = mysqli_fetch_array ($result)) {

echo "<tr><td>" . $row[0] . "</td><td>" .$row[1];
echo "</td><td>" . $row[2] . "</td></tr>";
}
echo "</table>";[/php]

The script for doing the maths is ok too.

[php]function distance($lat1, $lon1, $lat2, $lon2, $unit) {

$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;

    return $miles;

}
[/php]

Then the array. I think that this needs to be an associative array. The distances need to be stored in the array, I think. I am unsure how to code this, can’t seem to nail anything down. Maybe something like:
[php]
$distance = array();

$distance[]=>'$lat1'
$distance[]=>'$lat2'
$distance[]=>'$lat3'
$distance[]=>'$lat4' [/php]

Accidentally posted this without finishing - can’t seem to edit my post either or delete it.

Anyway, I know the array doesn’t work, if you can help me please do.

I’m happy for anyone to go back to my original code from my previous post and take it from there as well.

As always, any help is much appreciated!

Sorry for not responding, been crazy with family issues… Did you figure this out as yet?
If not, let me know where you are in the project and I can help…

Thank you for your reply.

To summarise the current structure:

  • Main.php – PHP page with map, input fields, submit button. Pulls in Google API key and Gmap.gs in order to calculate a Users longitude and latitude depending on the postcode that they have entered into a text box and puts the longitude and latitude into their respective text fields (‘lat’ and ‘lon’).

A second button sends the longitude and latitude to Distance.php (this is where the problems are occurring I think).

  • Gmap.js - A JavaScript file (that is pulled in by the PHP page).

Original source: http://www.tomanthony.co.uk/demo/geocode_uk_postcode/gmap.js

  • Distance.php – The second button aims to send the latitude and longitude of the users location from main.php over to this page and compare it to the values in the database. It should then tell the user what their nearest location is. Full code below.
    [php]
Query page <?php

function distance($lat1, $lon1, $lat2, $lon2, $unit) {

$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;

    return $miles;

}
print_r($_POST);
$lon = $_POST[‘lon’];
$lat = $_POST[‘lat’];
//$lon = ‘lon’;
//$lat = ‘lat’;
$con=mysqli_connect(‘localhost’, ‘username’, ‘password’) or die (‘No connection’);

mysqli_select_db($con, 'databaseName' ) or die ('test will not open');

    $query = "SELECT postcode_id, lat, lon FROM `postcodes`";

$result = mysqli_query($con, $query) or die("Invalid query");

echo "<table border='1'><tr><th>Location_ID</th><th>Lat</th>";
echo "<th>Lon</th></tr>";	

while ($row = mysqli_fetch_array ($result)) {

	$distance[$row[0]]=distance($lat,$lon,$row[1],$row[2],0);
	echo "<tr><td>" . $row[0] . "</td><td>" .$row[1];
	echo "</td><td>" . $row[2] . "</td></tr>";
	}
	echo "</table>";

print_r($distance);

$distance_a=$distance;

//sorting algorithm
asort($distance_a);

echo "<br />";
print_r($distance_a);

$i=0;

foreach($distance_a as $k=>$v){
	$final[$i]= $k;
	$i++;

}
echo "<br />";
echo "The id of the closest school is ".$final[0];


mysqli_close($con);

?>

[/php]

I’m using print_r ( to do amongst other things) to return the array of distances (in miles) between the User entry and the values in the postcode.
Some of these are returning correct values. (The distance is ‘as the crow files’ and I haven’t rounded it yet).

BUT its returning “8.3420528255351E-5” (why the “E-5” at the end?) for example when a postcode is entered that is actually in the database (so really it should be 0 miles).

Out of the 3 values in the database only one is coming out as expected. i.e. first in the array and “0” (miles).

Checked the longitude and latitude values in my database against what Main.php was outputting in the text fields and they matched so I don’t think the issue is here

Hope this makes sense, written as I was testing it. If there is anything I have not been clear of, please just ask and I will do my best to answer the question.

Still a bit of work to do but I don’t think I’m far off getting what I want.

Thank you for your time.

Well, I think you are nearly done. First, the “E-5” is an exponent of the number. This means you are using floating point values for you numbers. So, 4E+5 is 400000 and 4E-5 is .00004 or something like that… It means the number was too big for the byte size of the value stored, so they move the decimal point for you. So, “8.3420528255351E-5” is something like “.00008342058255351”. This number is not a rounded number but is over the size limit for the field. (Most likely 16 numbers!) This really does not matter as a number is a number and should not be a problem in your calculations. The issue comes when you display a floating number like this without formatting it first. You would just format this output when needed as integer with, let’s say, two decimals.
(Using number_format: http://php.net/manual/en/function.number-format.php

Now, my last longer post to you was not a recap of your post, it was a step-by-step instructional on what you needed to do in your “distance” routine. Here is the code you posted with some comments added and a few changes. (Comments were more for me!) Note, I decided that you need to do your sort first. If you use PHP, this is much much easier than your version. So, I sort the data pulled from your database while building a new array. This sort is just a couple lines of code. Then, when it is displayed, it does so by lowest distance first. (Top line is closest school!) I can not test this without access to your database. So, if it does not work, please private-message me the access info for your database or just post the errors here. Please try it and let us know if it works…
[php]

Query page <?php // Function to calculate distance between two points (*** Removed units, not used! ***) function distance($lat1, $lon1, $lat2, $lon2) { $theta = $lon1 - $lon2; $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $miles = $dist * 60 * 1.1515; return $miles; }

print_r($_POST);
$lon = $_POST[‘lon’];
$lat = $_POST[‘lat’];

// Get ALL lat-lon from database…
$con=mysqli_connect(‘localhost’, ‘username’, ‘password’) or die (‘No connection’);
mysqli_select_db($con, ‘databaseName’ ) or die (‘test will not open’);
$query = "SELECT postcode_id, lat, lon FROM postcodes";
$result = mysqli_query($con, $query) or die(“Invalid query”);

// Create new array from all the data pulled from database info…
$latlon_array = array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
$latlon_array[] = $row;

// Add the distance info and sort the data by it…
foreach ($latlon_array as $key => $row) {
$latlon_sorted[$key] = distance($lat,$lon,$row[1],$row[2]); // Set distance as key…
}
array_multisort($latlon_sorted, SORT_DESC, $latlon_array);

// Display ALL lat-lon…
echo “

”;
foreach ($latlon_sorted as $latlon_line) {
echo “”;
foreach ($latlon_line as $latlon_row) {
echo “”;
}
echo “”;
}
echo “
Location_ID Lat Lon
” . $latlon_row . “
”;
mysqli_close($con);
?> [/php] The major changes were using more standard layout of pull data, sort it, display it... Also, streamlined the display code a bit... Let me know if it works for you...

Thank you for your help - its getting there!

What access info would do you need for my database? Username and password or just the data for the two tables? Just let me know and I will send this info to you.

What I see when I submit to distance.php:

Array ( [lat] => 54.505857 [lon] => -6.033997 )

The following errors:

Warning: mysql_fetch_array() expects parameter 1 to be resource, object given in distance.php on line 25

Warning: array_multisort(): Argument #1 is expected to be an array or a sort flag in distance.php on line 33

Warning: Invalid argument supplied for foreach() in distance.php on line 37

And the following in a table but with no rows below it.

Location_ID Lat Lon

Well, I am sorry, I type all of those changes quickly and did not test it without recreating your set up on my database. It would be simpler if you private-message me the connection info and I will test it quickly and get it back to you. You can change the password later on. (This is of course if it is an online site, if it is a local site on your system, I can’t do it that way.) (‘localhost’, ‘username’, ‘password’)

I also think it is just the MySQL fetch-assoc issue… while I wait for you, I will look at it…

The website is not hosted online (not at that stage yet). I’m only using two tables for this so I will paste the SQL code below so that it can be easily imported into PHPMyAdmin.

I have edited some of the postcodes so I am not pasting the original postcodes and latitudes and longitudes. I was using my own house in one postcode (for quick and easy input of postcodes before getting the functionality workng) so I have edited this before putting it on a public forum.

--
-- Table structure for table `locations`
--

CREATE TABLE IF NOT EXISTS `locations` (
  `location_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `venue` varchar(50) NOT NULL,
  `address` varchar(50) NOT NULL,
  `district` varchar(50) NOT NULL,
  `postal` varchar(7) NOT NULL,
  `hours` varchar(100) DEFAULT NULL,
  PRIMARY KEY (`location_id`),
  KEY `postal` (`postal`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `locations`
--

INSERT INTO `locations` (`location_id`, `venue`, `address`, `district`, `postal`, `hours`) VALUES
(1, 'Lisburn School', 'Chapel Hill', 'Lisburn', 'BT275QB', 'Mon-Fri: 9-5, Sat: 8-6, Sun: Closed'),
(2, 'Carrickfergus School', 'Joymount', 'Carrickfergus', 'BT377DN', 'Mon-Fri: 9-5, Sat: 8-6, Sun: Closed'),
(3, 'Belfast School', 'Lisburn Road', 'Belfast', 'BT96GR', 'Mon-Fri: 9-5, Sat: 8-6, Sun: Closed');

-- --------------------------------------------------------
--
-- Table structure for table `postcodes`
--

CREATE TABLE IF NOT EXISTS `postcodes` (
  `postcode_id` int(11) NOT NULL AUTO_INCREMENT,
  `postcode` varchar(7) NOT NULL DEFAULT '',
  `lat` varchar(10) NOT NULL DEFAULT '',
  `lon` varchar(10) NOT NULL DEFAULT '',
  `district` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`postcode_id`),
  UNIQUE KEY `postcode` (`postcode`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

--
-- Dumping data for table `postcodes`
--

INSERT INTO `postcodes` (`postcode_id`, `postcode`, `lat`, `lon`, `district`) VALUES
(1, 'BT275QB', '54.500506', '-6.039802', 'Lisburn'),
(2, 'BT377DN', '54.677234', '-5.904398', 'Carrickfergus'),
(3, ' BT96GR', ' 54.570105', ' -5.962786, 'Belfast');

Okay, that post helps. I can figure it out for you, but, I have to leave for a few hours.
I will look at it when I get back and post to you… So, check later or in the morning…
Thanks for the additional info…

Just to reply to something you said an earlier post because it was relevant.

Now, my last longer post to you was not a recap of your post, it was a step-by-step instructional on what you needed to do in your "distance" routine.

I did try to follow the step-by-step guide but I just couldn’t get my head round it. A more experienced PHP programmer would probably have been able to decipher it a lot easier than I was. It would be very interesting to see what you come up with.

I noticed you mentioned the use of a ‘for loop’ in that post containing the step-by-guide but I could not see that in your code that you posted. This was maybe because you just adapted to what you saw in the code that I posted. But regardless of any of that I am interested to see your interpretation of this and subsequently and learn from it.

One note, in your INSERT command:
INSERT INTO postcodes (postcode_id, postcode, lat, lon, district) VALUES
(1, ‘BT275QB’, ‘54.500506’, ‘-6.039802’, ‘Lisburn’),
(2, ‘BT377DN’, ‘54.677234’, ‘-5.904398’, ‘Carrickfergus’),
(3, ’ BT96GR’, ’ 54.570105’, ’ -5.962786, ‘Belfast’);

There is a missing quote on the 3rd inserted line.

Well, Rudiger, this “little” script was a pain to sort out… But, SUCCESS! LOL…

First, it appears that there is no easy way to create the calculations inside the SQL. (I tried it several ways and could not have it work well.) So, I tried several ways to create a sortable array. I finally figured out the correct way to do it so it would not take a ton of code, but, work as you wanted it to. Note, I did not round off the miles and the numbers I used were just testing numbers, you have to change this to get from your posted numbers… Hope it works for you!

New version:
[php]

<?php $hostname = "xxxxxxxxx"; $username = "xxxxxxxxx"; $password = "xxxxxx"; $dbname = "xxxxx"; // Function to calculate distance between two points (*** Removed units, not used! ***) function distance($lat1, $lon1, $lat2, $lon2) { $theta = $lon1 - $lon2; $dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $miles = $dist * 60 * 1.1515; return $miles; } // $lon = $_POST['lon']; // $lat = $_POST['lat']; $lon = '54.5039'; //Test with some fake numbers for lon $lat = '6.0334'; //Test with some fake numbers for lat // Get ALL lat-lon from database... $con=mysql_connect($hostname, $username, $password) or die ('No connection'); mysql_select_db( $dbname ) or die ('test will not open'); $query = "SELECT postcode_id, lat, lon FROM postcodes"; $result = mysql_query($query) or die("Invalid query"); // Create new array from all the data pulled from database info... $latlon_array = array(); while ($row = mysql_fetch_assoc($result)) $latlon_array[] = $row; // Add the distance info into an array... $i=0; foreach ($latlon_array as $key => $row) { $latlon_sorted[$i][0] = $row['lat']; $latlon_sorted[$i][1] = $row['lon']; $latlon_sorted[$i][2] = distance($lat,$lon,$row["lat"],$row["lon"]); ++$i; } // Sort the final array... array_multisort($latlon_sorted, SORT_DESC, $latlon_sorted); // Display ALL lat-lon... echo ""; foreach ($latlon_sorted as $latlon_line) { echo ""; foreach ($latlon_line as $latlon_row) { echo ""; } echo ""; } echo "
Location_ID Lat Lon
" . $latlon_row . "
"; mysql_close($con); ?>

[/php]
Notes: I used standard MySQL, no MySQLi as I read it is not as quick. Also, to figure it out, I needed to use your data in my own database, so you have to alter the connection strings, etc… Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service