Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP/mysql sort results by value calculated by other database
Old 10-14-2010, 04:00 PM PHP/mysql sort results by value calculated by other database
Junior Talker

Posts: 1
Name: Nick
Trades: 0
I am a newbie when it comes to the PHP stuff. But I will explain what I am trying achieve and where i have go so far.

I am trying to make a search form which will search search a database with information on voluntary placements. The user will select there worktype, enter there postcode well the first part of it and select a radius of distance of where a placement is based.

I have been able to bring up search results of worktype, however for the postcode distance I have taken the postcode of a placement(which i got from the first database) and the postcode entered in the search form, then search another database to then get the coordinates of both postcodes. Then using the coordinates in a calculation to find the distance.

However I would like to know how or if it is possible to then sort the result from the first database by the distance which was calculated by taking the two postcodes.

Any hint or tip would be great
nick0488 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-14-2010, 05:34 PM Re: PHP/mysql sort results by value calculated by other database
tripy's Avatar
Do not try this at home!

Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
Trades: 0
I think you would be better to constitute a database yourself that would give distance between 2 locations.
Or, if you can get the latitude/longitude pair of those locations, the distance can be computed from the database itself.

Otherwise, if you can get a link of the 2 database, you could do an aggregation of the 2 results set, but most likely, you only have access to a front end of the distance database.

In this case, but it will be slow, what you need to do is
1) run your query on your local database
2) for each results (non paginated), do a query of the distance, and store the distance in an array like this:
PHP Code:
$ary[$distance][]=$rowId
This will construct an array, with the distances as a key, and every database rows id as an 2nd dimension array
3) sort your 1st dimension
4) display the results

Of course, if your user has 10 or so results, it will work.
But if he has 300, this will be so slow that he will close the browser before every distance have been computed.

So really, all you got to do is fetch the latitude/longitude of each locations you have in your db, and then the computation is a simple

Code:
SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon - lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance 
FROM yourTable
HAVING distance <= 10
ORDER BY distance DESC
Where $lon and $lat are the coordinate of the center of the radius (the origin)
http://zcentric.com/2010/03/11/calcu...and-longitude/
http://www.marketingtechblog.com/tec...late-distance/

Simply calculate this.
Create a table with every places, enter the latitude/longitude of each places (google hearth is handy to find them) and don't bother anymore with that second db.
__________________
Only a biker knows why a dog sticks his head out the window.
tripy is offline
Reply With Quote
View Public Profile Visit tripy's homepage!
 
Reply     « Reply to PHP/mysql sort results by value calculated by other database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.10115 seconds with 12 queries