Ok, i've been doing some playing around and got it to work. I thought just in case someone else is looking to do the same thing here's a simplified version of what i'm using:
$result = mysql_query($_SESSION[$search.'sql']) or trigger_error("SQL", E_USER_ERROR);
while ($list = mysql_fetch_assoc($result)) {
if($_POST['submit'] == "Show Distance"){
//If user has set postcode create distance info.
//Users postcode
$pcodeA = strstr($_SESSION['postcode'], ' ', true);
//Database Postcode
$pcodeB = strstr($row['postcode'], ' ', true);
//distance variable
$distance = calc_postcode_seperation($pcodeA,$pcodeB);
}else{
//User hasn't submitted distance so distance variable is blank
$distance = "";
};
$data[] = array('name' => $list['name'], 'distance' => $distance);
};
// Obtain a list of columns
foreach ($data as $key => $row) {
$name[$key] = $row['name'];
$distance[$key] = $row['distance'];
}
// Sort the data with volume descending, edition ascending
// Add $data as the last parameter, to sort by the common key
array_multisort($distance, SORT_DESC, $name, SORT_ASC, $data);
foreach($data as &$list){
echo $list['name']."<br>";
echo $list['distance']."miles";
};
};
If anyone has any suggestions to optimize it id love to know.