Jump to content


Photo

Results using x and y *SOLVED*


  • Please log in to reply
3 replies to this topic

#1 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 24 September 2006 - 04:50 PM

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

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

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

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 24 September 2006 - 05:49 PM

Good old Pythagoras!

<?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]

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 CanMan2004

CanMan2004
  • Members
  • PipPipPip
  • Advanced Member
  • 254 posts

Posted 24 September 2006 - 06:11 PM

That seemed to work a treat, thank you very much

#4 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 24 September 2006 - 06:35 PM

try
$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';





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users