Forum Moderators: coopster

Message Too Old, No Replies

how to sort distance?

how to sort or order distance?

         

phlogit

7:04 pm on Jun 7, 2007 (gmt 0)

10+ Year Member



Hi, firstly i would like to thank everyone that is all ready going to help me as you guys are great at helping from what i have read on these forums.
I am a newbie at php and would like to know how i would go about sorting ads by distance.

The distance is currently echo'd using the following method.

$d = round(distance($lati[0], $long[0], $lati[1], $long[1],"M"));

echo $d;

at the moment the $d is using distance which is a function that calculates the distance between two given postcodes using an sql table in the database. I have got it to work so far so that it displays the distance in miles beside each ad but would now like to know how I would go about ordering or sorting the ads by $d echo'd. As the resulting figure is not stored in a database I cannot use order by function provided by sql (at least I think I cant).

Thanx in advance.

hughie

8:16 pm on Jun 7, 2007 (gmt 0)

10+ Year Member



Hi phlogit

I reckon you want a query similar to this.

$Lat= your base latitude;
$Long=Your base Longitude;

$Sql="SELECT * ,
SQRT(((".$Lat."-tablename.Lat)*(".$Lat."-tablename.Lat)) + ((".$Lon." - tablename.Long)*(".$Lon." - tablename.Long))) AS TDistance
FROM tablename
WHERE
ORDER BY TDistance Asc";

$result=mysql_query($Sql)......etc

hughie

phlogit

8:22 pm on Jun 7, 2007 (gmt 0)

10+ Year Member



Im thinking something along the lines of sort($d).

Come on Mods help me you know you wana! Thanx in advance again.
I will pray for you if you help me.

phlogit

8:26 pm on Jun 7, 2007 (gmt 0)

10+ Year Member



thanx for replying hughie could you explain the code is it defining the $lat as the lat from the table same for $long and calling TDistance and then ordering it? am i right?

phlogit

9:06 pm on Jun 7, 2007 (gmt 0)

10+ Year Member



hughie help me man im dying here

hughie

9:58 pm on Jun 7, 2007 (gmt 0)

10+ Year Member



i hope it's not serious...

steps are thus:

get the lat/long of the point you want to sort from

Bills Shop Lat: 1000, Long 2000;

save those as $lat,$lon, then run the query from that, it will generate the distance from that point save it within the call as TDistance which it's being sorted by.

saves pulling all the data out of the database and doing another sort on it.

phlogit

12:13 am on Jun 8, 2007 (gmt 0)

10+ Year Member



i see what you mean hughie but it doesnt seem to be working i think its the sql query something is not right.

so say i got 3 ads as follows

----------ad1---------------------------------------
details of ad 10 miles away
----------------------------------------------------
----------ad2---------------------------------------
details of ad2 5 miles away
----------------------------------------------------
----------ad3---------------------------------------
details of ad3 14 miles away
----------------------------------------------------

The postcode i enter is $postcode1 and the postcodes in the 3 ads are defined as $postcode2. so i then use 2 sql queries to find the longitude and latitude for both postcodes from my table of postcodes in the database. i retrieve the corresponding lats and long for both postcodes and call them

$lati[0], $long[0] for $postcode1
$lati[1], $long[1] for $postcode2

i then with longs and lats above calculate using function distance to calculate the distance between the longs and lats.

$d = round(distance($lati[0], $long[0], $lati[1], $long[1],"M"));

echo $d;

function is defined after round(..

this in return echos the miles figures u see above 3 ads. now wot i need to do is sort these nearest first but cant do it through sql because the function distance is in php.

if you need more clarification then please ask i will try to explain better. thanx in advance.

Hughie- i see you use the postcode calc on your website so maybe you can help further.

Note: I've started self-harming.

eelixduppy

1:16 pm on Jun 8, 2007 (gmt 0)



If you want to sort them using your php function you are going to have to push all of the distances into an array and sort them using sort [php.net](), however, you are going to have to keep track of which ad belongs to which distance. For this, I would set the index for each element of the distance array to something unique for each ad.

It would be better, though, if you can calculate the distances with mysql to save you a step in the process and make it easier. Not sure if you have the ability to do this, but it seems like it would be a better solution.

Try to get something together and then post the relavent code, and we will see what we can do from there.

phlogit

1:40 pm on Jun 8, 2007 (gmt 0)

10+ Year Member



Thanx for the guidance eelixduppy. As i am new to php and sql for that matter i will try your first suggestion as i think it will be too difficult for me to create a query that works and will calculate the distance.

$lati[1]=$row[latitude];
$long[1]=$row[longitude];

$distance = round(distance($lati[0], $long[0], $lati[1], $long[1], "M"));

$nearest_distance[] = $distance;

sort($nearest_distance);

echo $nearest_distance[0];

As the above code shows im trying the sort method now it does do some kind of sort with the distance but the ads dont sort and i think the distance just get sorted but the ads dont move around or get sorted (i think this is where i need the unique index thing for each ad that eelixduppy mentioned). Btw totaly new to arrays too so dont laugh, actually laugh cos i am.

Any help greatly appreciated and still on my knees praying.

phlogit

4:05 pm on Jun 8, 2007 (gmt 0)

10+ Year Member



BUMP STILL HERE

phlogit

9:30 pm on Jun 8, 2007 (gmt 0)

10+ Year Member



Right i am trying to use id of each ad to index elements of distance but dont have a clue on how to do this. The code below is what I have gathered so far but it doesnt do anything lol something is wrong somewhere. HELLLLLPPP!

$query = "select * from ads";
$result = mysql_query($query) or die('Error : ' . mysql_error());
$row = mysql_fetch_array($result);
$key=$row[id];

$nearest_distance = array();

$lati[1]=$row[latitude];
$long[1]=$row[longitude];

$distance = round(distance($lati[0], $long[0], $lati[1], $long[1], "M"));

$nearest_distance[$key] = $distance;

sort($nearest_distance);

echo $nearest_distance[0];

jatar_k

3:03 pm on Jun 9, 2007 (gmt 0)