1
 mysql_query("SELECT * FROM users WHERE level = 7 ");

After querying the database, I have a list of level 7 users (installers). I use a while() loop to display all the installers. Each installer has a post code which, using PHP and GoogleMaps, is compared against the postcode of the currently logged in customer and returns the distance between the two post codes.

How can I sort this list of installers by the newly created "distance" variable?

1
  • 1
    Required output and table schema must have been shared to get a better answer. Commented May 22, 2015 at 14:07

1 Answer 1

2

http://php.net/manual/en/function.array-multisort.php

// Obtain a list of columns
foreach ($data as $key => $row) {
    $volume[$key]  = $row['volume'];
    $edition[$key] = $row['edition'];
}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($volume, SORT_DESC, $edition, SORT_ASC, $data);

Is the example your looking for. You only have 1 key though

// Obtain a list of columns
foreach ($data as $key => $row) {
    $var[$key]  = $row['your_new_var'];

}

// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($var, SORT_DESC, $data);
?>

Where your_new_var is the key sorting on

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.