powelly Posted May 3, 2007 Share Posted May 3, 2007 The Plan: 1, User enters postcode and clicks submit 2, stuff happens 3, User is presented with a list of locations sorted by order or distance from the user Ok so far based on www.pjenkins.co.uk/uk_postcodes 1, I have a form user enters postcode 2, the for each entry is the location table I calculate the distance from the user and write a row to a table 3, Result I have a list of locations in the order they are in the table, not distance order. My while loop looks like this while($row = mysql_fetch_row($result)) { echo "<tr>"; $query2 = 'SELECT `Lat`, `Long` FROM `UKPostCodes` WHERE `PostCode`="'.$row[2].'";'; $result2 = mysql_query($query2); $second = mysql_fetch_row($result2); $distance = "The distance from postcode: $firstpc is ".getDistance($first[0], $first[1], $second[0], $second[1])." miles."; echo "<td>$row[1]</td>";//Location Name echo "<td>".$distance."</td>"; echo "</tr>"; } I thinking i need to assign each result to a array, and then display the arrays sorted by distance rather than drawing the table but would appreciate some pointers. Thanks in advance Powelly Quote Link to comment Share on other sites More sharing options...
techtheatre Posted May 3, 2007 Share Posted May 3, 2007 Yes. You should be able to write the values into an array and then sort the array and display the results. Check this page for info on sorting arrays: http://us2.php.net/sort Quote Link to comment Share on other sites More sharing options...
techtheatre Posted May 3, 2007 Share Posted May 3, 2007 one other thought...the script you submitted is missing the first SQL query, but you could potentially pull all the data you need using one (nested) SQL query...and get it in order by including an "ORDER BY" clause in the SQL statement. Google "Nested SQL". Quote Link to comment Share on other sites More sharing options...
powelly Posted May 3, 2007 Author Share Posted May 3, 2007 one other thought...the script you submitted is missing the first SQL query, but you could potentially pull all the data you need using one (nested) SQL query...and get it in order by including an "ORDER BY" clause in the SQL statement. Google "Nested SQL". That would be nice but the distance is not stored in the database, its calculated for each row returned in the first select query, so for each row returned I have the data from the table and a variable of the distance, so i need to put these two elements together and then display them by distance order. Quote Link to comment Share on other sites More sharing options...
techtheatre Posted May 3, 2007 Share Posted May 3, 2007 cool...then an array is your best option. you can have a single array that has multiple keys (one for distance, one for location name, etc). then sort the array and use a foreach loop to pull this data back out and display it on the screen. Quote Link to comment Share on other sites More sharing options...
powelly Posted May 3, 2007 Author Share Posted May 3, 2007 New to arrays so reading the multidimensional arrays just went well over my head. <?php $shop = array( array("rose", 1.25 , 15), array("daisy", 0.75 , 25), array("orchid", 1.15 , 7) ); ?> So I could go <?php $locations = array( array("Manchester", 4), array("Oldham", , array("Rochdale", 11) ); ?> this makes sense, but how do i do it within my loop so as I go through the results in the table I add another row to the array? 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.