Re: Sorting an Array - CSORT



Nel wrote:
Can you post the all code you're having trouble with, instead of just a couple of lines?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@xxxxxxxxxxxxx
==================


Here is the code I am using - I have managed to get the code to re-create two temp arrays and output the distance and userid in distance order, but this seems very messy and longwinded.

See below

<?php

mysql_select_db($database_exampleDB, $exampleDB);
$query_numUsers = sprintf("SELECT dest.outcode, dest.x, dest.y, u.* FROM
postcodes AS source, postcodes AS dest, hotels AS u WHERE
source.outcode ='%s' AND ((dest.x <= (source.x + %s) AND dest.x >= (source.x -
%s)) AND (dest.y <= (source.y + %s) AND dest.y >= (source.y - %s))) AND
u.outcode = dest.outcode AND u.fullname != '%s' ORDER BY dest.outcode ASC",
$colname_numUsers,$r_numUsers,$r_numUsers,$r_numUsers,$r_numUsers,$user_numUsers);
$numUsers = mysql_query($query_numUsers, $exampleDB) or die(mysql_error());


$row_numUsers = mysql_fetch_assoc($numUsers);
$totalRows_numUsers = mysql_num_rows($numUsers);

$sql = "SELECT * FROM postcodes WHERE outcode = '$Soutcode'";
$sqlresult = mysql_query($sql,$exampleDB) or die(mysql_errno());
$r = mysql_fetch_array($sqlresult);
$num_rows = mysql_num_rows($sqlresult);

$locX = $r[x];
$locY = $r[y];

echo "$totalRows_numUsers<p>";

if ($totalRows_numUsers > 0) { $temp = mysql_data_seek($numUsers, 0); }
$temp = 0;
while ($row_numUsers = mysql_fetch_assoc($numUsers)) {
 $temp++;
 $lid = $row_numUsers[id];
 $netX = ($row_numUsers[x] - $locX);
 $netY = ($row_numUsers[y] - $locY);
 $hypotm = sqrt(($netX * $netX) + ($netY * $netY));
 $hypotm = $hypotm / 1000 * 0.62 * 1.3;
 $hypotm  = number_format($hypotm,1);
 $distance[$lid] = $hypotm;
 $clist[$temp] = $lid;
}

if ($totalRows_numUsers > 0) { $temp = mysql_data_seek($numUsers, 0); }
$row_numUsers = mysql_fetch_assoc($numUsers);

array_multisort($distance, SORT_ASC, $clist);

$temp = 0;
foreach($distance as $dvalue) {

   echo $dvalue." - ".$clist[$temp]."<br>";

   $temp++;
}


?>

Outputs:

6

2.1 - 6    (distance - userid)
2.1 - 106
55.2 - 71
55.9 - 26
57.1 - 85
58.9 - 24





Yep, quite convoluted.

As a suggestion - just read everything into an array, compute your distance for each one and then sort. For instance (code not even syntax checked, but you get the idea)...

function comp ($a, $b) {
  return $b['dist'] - $a['dist']; // Reverse to sort descending
}

$sql = "SELECT * FROM postcodes WHERE outcode = '$Soutcode'";
$sqlresult = mysql_query($sql,$exampleDB);
if ($sqlresult) // or die() is SO user unfriendly!
  $r = mysql_fetch_array($sqlresult);

(your big query here)
  $numUsers = mysql_query($query_numUsers, $exampleDB);
  if ($numUsers) {  // or die is SO user unfriendly!
    $i = 0;
    while ($row_numUsers = mysql_fetch_array($numUsers) {
      $all_numUsers [$i] = $row_numUsers;
      $netX = ($row_numUsers[x] - $locX);
      $netY = ($row_numUsers[y] - $locY);
      $all_numUsers[$i++]['dist'] =
        (sqrt(($netX * $netX) + ($netY * $netY))) / 1000 * 0.62 * 1.3
    }
    usort ($all_numUsers, 'cmp');
    // Rest of your work
  }
  else ( // numUsers query failed
    // Output an error message here
  }
}
else { // Postcode query failed
  // Output another error message here
}


-- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@xxxxxxxxxxxxx ================== .