Jump to content

postcode distance from and sorting the results


powelly

Recommended Posts

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

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".

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.

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.

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?

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.