Jump to content

Sorting sql results in an array using things outside the array to sort them?


TehDooMCat

Recommended Posts

Being a relative noob to SQL/PHP, this problem is hurting my brain.

 

I'm using PHP to connect to a database full of place names, and their associated post codes, latitude and longitude on the earth. A user types his or her post code into a form, plus a distance (in miles), and the script looks for other area codes (first half of the post code) within that distance.

 

I can do that fine, but the results are ordered alphabetically by postcode, and I want to order it by the distance. Problem here, is that the distance isn't part of the array taken from the database, it takes the latitude and longitude of the area it's on in the while loop, and calculates the distance between the user's post code and that one, but it's not part of the array, so I can't sort the array using it.

 

How can I sort it? Can I add the calculated distances to the array, and sort it using sort()? If yes, how do I add them to the array?

 

Here's the page, just to explain better what the script does: http://navimaker.org/ed/owain/results.php?postcode=gl15&distance=34

Mess around with the postcode and distance vars.

 

I'd put the source on here too but I'm doing all this over SSH and can't copy & paste very easily :-\

 

Also, sorry if this is a double thread, I've tried to post three times and it still isn't coming up :-\

Link to comment
Share on other sites

Okay, cool, the extra value's in an array now :D

 

But I forgot to mention - the whole thing (was) in a while loop. I've got it to show the results inside another while loop afterwards, and it sorts the array in between the two loops. But in the second loop it does nothing.

 

Here's the code:

echo "<br /><b>Nearest postcodes:</b><hr noshade /><table border='0' width='100%'>";
$x = 0;

while ($row = pg_fetch_row($outcode)) {
        $dist = 3963 * acos(sin(deg2rad($inlat)) * sin(deg2rad($row[2])) + cos(deg2rad($inlat)) * cos(deg2rad($row[2])) * cos(deg2rad($inlong) -
deg2rad($row[1])));
        $dist = round($dist);
        $row[4] = $dist;
        //$row = array_multisort($row, $diff);
        //echo "<tr><td>" . $row[0] . "</td><td>" . $row[3] . "</td><td>" . $row[4] . "</td></tr>";
}

$i = 0;
while( $i <= sizeof($row) ) {
echo "<tr>" . $row[0][$i] . "</td><td>" . $row[3][$i] . "</td><td>" . $row[4][$i] . "</td></tr>";
$i++;
}
echo "</table>";

 

It's trying to do something in the second loop - it's coming up with a row of blank columns, looking at the sourcecode, so there's something wrong with the counter or the numbers or something?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.