Jump to content

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
Link to comment
https://forums.phpfreaks.com/topic/21862-results-using-x-and-y-solved/
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]
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]
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.