Forum Moderators: coopster
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.
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
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.
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.
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.
$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.
$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];