Jump to content

Archived

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

CanMan2004

Results using x and y *SOLVED*

Recommended Posts

Hi all

I have a php page which allows users to register, when they register, they enter their name, area name such as London and then there x and y co-ordinates. This informaiton is then store in a sql database.

A sample of the database is

name,areaname,x,y,latitude,longitude
john,london,443400,346400,53.013,-1.353
andy,scotland,342600,462000,54.051,-2.877
sarah,wales,510700,195500,51.647,-0.4

Currently I have a php search form which performs a simple query on the register sql table and returns a result which match the users area name, for example, if someone searches for

"London"

then it performs the query

[code]$areaname = $_GET['area'];
$sql = "SELECT * FROM registered WHERE `areaname` >= '".$areaname."";[/code]

There is only ever one person in each area, so it only ever returns one result.

What I want to do is to run the above query, and then, after that, run another query which would return the next 10 rows which are closest to the row which has been found based on the x and y coordinates which have been inputted into each row in the database.

I'm not 100% if using the x and y coordinates will return the closest, but I believe it will.

I also store the latitude and longitude values in each row if anyone thinks that would help return a more accurate result.

Any help would be great as ive been trying to crack this for the last 18 hours and im going no where quick.

Thanks in advance

Dave

Share this post


Link to post
Share on other sites
Good old Pythagoras!

[code]<?php
$targetX = 443400;
$targetY = 346400;

$sql = "SELECT *, SQRT(POW((x - $targetX),2) + POW((y - $targetY),2)) AS dist
      FROM mytable
      ORDER BY dist
      LIMIT 10";
?>[code][/code][/code]

Share this post


Link to post
Share on other sites
try[code]$sql = 'SELECT @x := x, @y := y'
        . ' FROM registred'
        . ' WHERE areaname = ''london'';'
        . ' SELECT *, ('
        . ' x - @x'
        . ' ) * ( x - @x ) + ( y - @y ) * ( y - @y ) AS distanc FROM `registred`'
        . ' ORDER BY distanc ASC'
        . ' LIMIT 0 , 10';[/code]

Share this post


Link to post
Share on other sites

×

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.