472,796 Members | 2,256 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,796 software developers and data experts.

PHP & MySQL - connecting two databases

Nel
I am trying to understand the best way to extract a list of users from a
table based upon their location.

To do this I have a table containing all the UK postcodes with a grid
reference x & y.
postcode x y
AB10 392900 804900
AB11 394500 805300
AB12 393300 801100
AB13 385600 801900
AB14 383600 801100
AB15 390000 805300
AB16 390600 807800
AB21 387900 813200
AB22 392800 810700
AB23 394700 813500
AB25 393200 806900
AB30 370900 772900

In a seperate table I have a list of users including a postcode.
id firstname postcode
1 Heather BH4
2 Vicky OL1
3 Paola CF8
4 Joanna W14
5 Steve BD13
6 Sally NN1

In short I would like your opinoin on how best (most efficiently) to
calculate the nearest 20 users. The method I am using to calculate the
distance between the users and any chosen postcode is good old Pythagoras
dist = sq root ( (userX * userX) + (userY * userY) - (locationX * locationX)
+ (locationY * locationY) )

It seems that looping through the users to first calculate the distance is
essential. After that point I am not sure about the besr way to proceed.
Do I write the results to a temp table (with distances) then call them in
order of distance or is there a better way?

Thanks Nel.
Oct 10 '05 #1
4 1809
Nel wrote:
I am trying to understand the best way to extract a list of users from a
table based upon their location.

To do this I have a table containing all the UK postcodes with a grid
reference x & y.
postcode x y
AB10 392900 804900
AB11 394500 805300
AB12 393300 801100
AB13 385600 801900
AB14 383600 801100
AB15 390000 805300
AB16 390600 807800
AB21 387900 813200
AB22 392800 810700
AB23 394700 813500
AB25 393200 806900
AB30 370900 772900

In a seperate table I have a list of users including a postcode.
id firstname postcode
1 Heather BH4
2 Vicky OL1
3 Paola CF8
4 Joanna W14
5 Steve BD13
6 Sally NN1

In short I would like your opinoin on how best (most efficiently) to
calculate the nearest 20 users. The method I am using to calculate the
distance between the users and any chosen postcode is good old Pythagoras
dist = sq root ( (userX * userX) + (userY * userY) - (locationX * locationX)
+ (locationY * locationY) )

It seems that looping through the users to first calculate the distance is
essential. After that point I am not sure about the besr way to proceed.
Do I write the results to a temp table (with distances) then call them in
order of distance or is there a better way?

Thanks Nel.


Nel,

First of all, you have a problem with your equation. It assumes 1
degree if longitude has the same distance as 1 degree of latitude -
which only occurs at the equator. Everywhere else, 1 degree of
longitude is less than 1 degree of latitude. I don't have the correct
equation handy, but I found it one time quite easily with a google search.

Once you have that straightened out, you can do it this way - but it's
the hard way. Instead, let the database help you out.

For instance, say you want everything within 25 miles. In your program,
define a 50x50 mi. square with the "from" point in the center. That
is, find the longitude 25 mi. east and west with no change in latitude.
Then find the maximum and minimum latitude with no change in longitude.

This now gives you a range that all the target post codes must be in.
Now let the database do the work for you. Search the database for all
postcodes where the longitude is between the min and max above, and the
latitude is within the min and max.

Once you have this (much smaller) set of postcodes, you can run your
equation again against every one to determine if it is within the 25
mile radius circle.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 10 '05 #2
Nel
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:ku********************@comcast.com...
Nel wrote:
I am trying to understand the best way to extract a list of users from a
table based upon their location.

To do this I have a table containing all the UK postcodes with a grid
reference x & y.
postcode x y
AB10 392900 804900
AB11 394500 805300
AB12 393300 801100
AB13 385600 801900
AB14 383600 801100
AB15 390000 805300
AB16 390600 807800
AB21 387900 813200
AB22 392800 810700
AB23 394700 813500
AB25 393200 806900
AB30 370900 772900

In a seperate table I have a list of users including a postcode.
id firstname postcode
1 Heather BH4
2 Vicky OL1
3 Paola CF8
4 Joanna W14
5 Steve BD13
6 Sally NN1

In short I would like your opinoin on how best (most efficiently) to
calculate the nearest 20 users. The method I am using to calculate the
distance between the users and any chosen postcode is good old Pythagoras
dist = sq root ( (userX * userX) + (userY * userY) - (locationX *
locationX) + (locationY * locationY) )

It seems that looping through the users to first calculate the distance
is essential. After that point I am not sure about the besr way to
proceed. Do I write the results to a temp table (with distances) then
call them in order of distance or is there a better way?

Thanks Nel.
Nel,

First of all, you have a problem with your equation. It assumes 1 degree
if longitude has the same distance as 1 degree of latitude - which only
occurs at the equator. Everywhere else, 1 degree of longitude is less
than 1 degree of latitude. I don't have the correct equation handy, but I
found it one time quite easily with a google search.


The figures are in metres, not degrees so hopefully this will not be a
problem.

For instance, say you want everything within 25 miles. In your program,
define a 50x50 mi. square with the "from" point in the center. That is,
find the longitude 25 mi. east and west with no change in latitude. Then
find the maximum and minimum latitude with no change in longitude.
Using a square would make it easier. I had visioned calculating the
hypotinuse for each member.

This now gives you a range that all the target post codes must be in. Now
let the database do the work for you. Search the database for all
postcodes where the longitude is between the min and max above, and the
latitude is within the min and max.
Genius. So I can narrow down the processor work to a limited few.
Once you have this (much smaller) set of postcodes, you can run your
equation again against every one to determine if it is within the 25 mile
radius circle.


One final question, once I have narrowed down the sqlresult to those 25, and
I calculate the distance, how do I sort the sql results in the order of the
distance calculation?

Thanks,

Nel.
Oct 10 '05 #3
Nel wrote:
"Jerry Stuckle" <js*******@attglobal.net> wrote in message
news:ku********************@comcast.com...

Nel,

First of all, you have a problem with your equation. It assumes 1 degree
if longitude has the same distance as 1 degree of latitude - which only
occurs at the equator. Everywhere else, 1 degree of longitude is less
than 1 degree of latitude. I don't have the correct equation handy, but I
found it one time quite easily with a google search.

The figures are in metres, not degrees so hopefully this will not be a
problem.


Metres from where? Some central point (i.e. Greenwich Observatory)?
For a (relatively) small country like GB, it probably wouldn't be that
much of a problem. But for Russia or Canada it could be a problem :-)
For instance, say you want everything within 25 miles. In your program,
define a 50x50 mi. square with the "from" point in the center. That is,
find the longitude 25 mi. east and west with no change in latitude. Then
find the maximum and minimum latitude with no change in longitude.

Using a square would make it easier. I had visioned calculating the
hypotinuse for each member.


You will have to do that eventually (not all locations in the 25km.
square will be within a 25 km. circle).

This now gives you a range that all the target post codes must be in. Now
let the database do the work for you. Search the database for all
postcodes where the longitude is between the min and max above, and the
latitude is within the min and max.

Genius. So I can narrow down the processor work to a limited few.


Definitely. Calculations such as this are very CPU intensive. You want
to perform the calculation on as few items as necessary.
Once you have this (much smaller) set of postcodes, you can run your
equation again against every one to determine if it is within the 25 mile
radius circle.

One final question, once I have narrowed down the sqlresult to those 25, and
I calculate the distance, how do I sort the sql results in the order of the
distance calculation?


You really can't, because you can't determine the actual distance until
you perform the final calculations. I'd suggest placing them in an
array of postalcode=>distance and use asort to perform the sort.
Thanks,

Nel.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Oct 10 '05 #4
You might be interested in taking a peek at the Location module for
Drupal...

http://drupal.org/project/location

Actually USING the whole thing will most likely be overkill. But the
source code may prove useful.

Chow,
GC
In article <43***********************@ptn-nntp-reader04.plus.net>,
ne***@ne14.co.NOSPAMuk says...
I am trying to understand the best way to extract a list of users from a
table based upon their location.

Oct 11 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Mr Dygi | last post by:
Hi, PHP 4.3.4 installed manually from package *.zip and Apache 2. I have a problem with this simple code: <?php $link = mysql_connect("127.0.0.1","","") or die("Could not connect: " ....
1
by: Ken | last post by:
First time I have installed mysql. I have not been able to connect to the server/database with a new php installation. I have installed IIS. Do I need this for mysql? The php works fine. ...
0
by: sean peters | last post by:
Hi all, i've been weighing the pros and cons of running multiple concurrent mysqld's on one server, to have better control over what databases are on what physical disks. System: 4 processor sun...
3
by: Pugi! | last post by:
On a freshly installed Fedora C3 (incl. webserver apache php mysql) i get the following problem when connecting to mysql through a browser (phpMyAdmin): : #2002 Can't connect to local MySQL...
4
by: MLH | last post by:
A programmer developed an AMP (Apache/MySQL/PHP) application for me. When he was done, he sent me the PHP files and the MySQL dump file. Now, when I connect to the application on my LAN using...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
9
by: noor.rahman | last post by:
I was wondering how it may be possible to query 2 MySQL databases using one query statement from PHP. For instance: SELECT database1.tableA.field1 UNION database2.tableB.field2. My concern...
4
jepler
by: jepler | last post by:
I find myself in a circular reference loop that I can't seem to resolve. I recently upgraded from MySQL 4.0.x to MySQL 4.1.21 on OS 10.3.9 Server. Then, I *thought* I set my open master root password...
39
by: alex | last post by:
I've converted a latin1 database I have to utf8. The process has been: # mysqldump -u root -p --default-character-set=latin1 -c --insert-ignore --skip-set-charset mydb mydb.sql # iconv -f...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.