TehDooMCat Posted July 12, 2007 Share Posted July 12, 2007 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 :-\ Quote Link to comment Share on other sites More sharing options...
per1os Posted July 12, 2007 Share Posted July 12, 2007 <?php $array['distance'] = get_distance($long, $lat); $array[]['distance'] = get_distance($long, $lat); ?> Depending if you have a multi-dimm array or not. Quote Link to comment Share on other sites More sharing options...
TehDooMCat Posted July 12, 2007 Author Share Posted July 12, 2007 Okay, cool, the extra value's in an array now 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? 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.