CanMan2004 Posted September 24, 2006 Share Posted September 24, 2006 Hi allI 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 isname,areaname,x,y,latitude,longitudejohn,london,443400,346400,53.013,-1.353andy,scotland,342600,462000,54.051,-2.877sarah,wales,510700,195500,51.647,-0.4Currently 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 advanceDave Quote Link to comment https://forums.phpfreaks.com/topic/21862-results-using-x-and-y-solved/ Share on other sites More sharing options...
Barand Posted September 24, 2006 Share Posted September 24, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/21862-results-using-x-and-y-solved/#findComment-97648 Share on other sites More sharing options...
CanMan2004 Posted September 24, 2006 Author Share Posted September 24, 2006 That seemed to work a treat, thank you very much Quote Link to comment https://forums.phpfreaks.com/topic/21862-results-using-x-and-y-solved/#findComment-97662 Share on other sites More sharing options...
sasa Posted September 24, 2006 Share Posted September 24, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/21862-results-using-x-and-y-solved/#findComment-97678 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.