pocobueno1388 Posted January 10, 2011 Share Posted January 10, 2011 I successfully found some code that will display all users within a certain radius of a specific zip code. Now I'm trying to order the users it returns sorted by distance from the same zip code. Here is the query that gets me the list of zipcodes that are within a certain radius of another zip code - SELECT distinct(zipcode) FROM zipcodes WHERE (3958*3.1415926*sqrt((latitude-'.$lat.')*(latitude-'.$lat.') + cos(latitude/57.29578)*cos('.$lat.'/57.29578)*(longitude-'.$lon.')*(longitude-'.$lon.'))/180) <= '.$radius Now here is my query that selects the users that have matching zip codes from the ones the previous query returns - SELECT name FROM users WHERE zip IN (".implode(",", $zips).")" So again, I'm trying to order the last query by distance from a specified zip. I have the long/lat of all the zipcodes if that's needed. I feel like I need something like ORDER BY zip ASC STARTING WITH [this zip code] I'm not quite sure how to translate that to the query. Thanks for the help! Quote Link to comment Share on other sites More sharing options...
suresh_kamrushi Posted January 10, 2011 Share Posted January 10, 2011 You can use customer order query "ORDER BY FIELD" Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 10, 2011 Share Posted January 10, 2011 Try this and see how it works for you. $query = "SELECT distinct(zipcode), (3958*3.1415926*sqrt((latitude-($lat))*(latitude-($lat)) + cos(latitude/57.29578)*cos(($lat)/57.29578)*(longitude-($lon))*(longitude-($lon)))/180) AS `distance` WHERE (3958*3.1415926*sqrt((latitude-($lat))*(latitude-($lat)) + cos(latitude/57.29578)*cos(($lat)/57.29578)*(longitude-($lon))*(longitude-($lon)))/180) <= $radius ORDER BY `distance` ASC"; Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 10, 2011 Share Posted January 10, 2011 You can use customer order query "ORDER BY FIELD" Huh? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted January 11, 2011 Author Share Posted January 11, 2011 Thanks Pikachu2000, but that is only going to order the zip codes by distance, which I believe it's already doing. Either way, even if those come out in the correct order it still doesn't make my second query come out in the correct order. Can you see a way to combine the first and second query so I can select the users information as well? I know there is a way to combine it, but it's just too much for me to wrap my head around! Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 11, 2011 Share Posted January 11, 2011 It wasn't clear whether you had the ORDERing taken care of or not, but if it turns out you need that, I've tested it against one of my databases, and it does work. As far as the rest of your request, I'm probably not the right one to try to answer it. I suspect it could be done with a JOIN, but that isn't one of my strong points. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted January 11, 2011 Author Share Posted January 11, 2011 I played with it for a while, but I still can't figure it out Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted January 12, 2011 Author Share Posted January 12, 2011 Anyone else want to give it a shot? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted January 14, 2011 Author Share Posted January 14, 2011 Still stuck =/ Quote Link to comment Share on other sites More sharing options...
fenway Posted January 14, 2011 Share Posted January 14, 2011 Sorry, what's the outstanding issue? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted January 15, 2011 Author Share Posted January 15, 2011 Fenway - I have a query that gets all the users in a specified radius of a given zip code. Now I'm just trying to order the users by distance from that zip code. My first query just selects all the zip codes within that radius, then I am using a second query to select all the users with a matching zip code. I'm sure both of these queries can be combined and it will make it simpler to sort it, but I can't quite wrap my head around all of it. Quote Link to comment Share on other sites More sharing options...
fenway Posted January 19, 2011 Share Posted January 19, 2011 Sounds like a simple join on ZIP. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted January 30, 2011 Author Share Posted January 30, 2011 fenway - I'm still not quite sure how/if that would work in my case. Yes, that would join the queries but wouldn't give me my desired results. I'm really not even sure if the queries can be combined and get the results I want. I need to first get all the zipcodes that are in the radius, THEN get all the users with those zipcodes and order them by distance from the currently logged in users zipcode. So lets say I had the following zipcodes returned from the first query and the logged in users zipcode was 76017: 76015 76019 76008 76028 I would want them ordered like: 76015, 76019, 76008, 76028 Because those are ordered from closest to furthest from the users zipcode. Does this make sense? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 8, 2011 Author Share Posted February 8, 2011 I was finally able to figure it out. What I did was find the equation for getting the distance in miles, then just sorted them by distance. Here is the query for anyone else who needs help with this: SELECT (((acos(sin((".$latitude."*pi()/180)) * sin((z.latitude*pi()/180))+cos((".$latitude."*pi()/180)) * cos((z.latitude*pi()/180)) * cos(((".$longitude."- z.longitude)*pi()/180))))*180/pi())*60*1.1515) as distance, u.name FROM `zipcodes` z RIGHT JOIN users u ON u.zip = z.zipcode HAVING distance <= ".$distance." ORDER BY distance Quote Link to comment 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.