For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > November 2005 > Sorting an Array - CSORT









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Sorting an Array - CSORT
Nel

2005-11-28, 6:57 pm

Hi all,

I am using php and mysql to search through a database of stores for the
nearest store to a given postcode.

I have managed to limit the selection to the nearest stores (roughly within
a given area), but once I have the array from mysql I need to do a
calculation to find the number of miles in between the store and the user's
postcode ($distance). I can do this by looping through the array.

Finally I need to sort the array by $distance, ASC. This is where I am
failing. I have read the FM and Googled. The best result is the one below
which seemed to do what I needed in terms of the search, but it gives an
error on the line

foreach($array as $row) {

Not sure why?

I am using PHP Version 4.3.8 on Windoze.

Nel.

________________________________________
_______
POST FROM 2004 comp.lang.php
________________________________________
_______

You can use array_multisort() to create a kind of column sort function:

function csort($array, $column) {
$s = array();
foreach($array as $row) {
$s[] = $row[$column];
}
array_multisort($s, SORT_ASC, $array);
return $array;

}


Example:

$foo = array(
array('firstname' => 'foo', 'lastname' => 'bar'),
array('firstname' => 'bar', 'lastname' => 'foo')
);

print_r(csort($foo, 'firstname'));

Output:

Array
(
[0] => Array
(
[firstname] => bar
[lastname] => foo
)

[1] => Array
(
[firstname] => foo
[lastname] => bar
)

)

HTH
Micha


Nel

2005-11-28, 6:57 pm

"Ewoud Dronkert" <firstname@lastname.net.invalid> wrote in message
news:42rmo1pqlo1isau2gsjdjvqrhikkg5onmi@
4ax.com...
> Nel wrote:
>
> From the FM at http://php.net/foreach : "foreach works only on arrays, and
> will issue an error when you try to use it on a variable with a different
> data type or an uninitialized variable."
>
> --
> E. Dronkert


Thanks - I have the routine working but it doesn't do what I need.

I am trying to sort a multi-dimentional(?) array returned from mysql (i.e.
rows of data) based upon a calculation.


get result from mysql

sort through and calculate $distance($id) for each store, then sort the
result according to distance ASC.

list stores in order.


Jerry Stuckle

2005-11-28, 9:56 pm

Nel wrote:
> "Ewoud Dronkert" <firstname@lastname.net.invalid> wrote in message
> news:42rmo1pqlo1isau2gsjdjvqrhikkg5onmi@
4ax.com...
>
>
>
> Thanks - I have the routine working but it doesn't do what I need.
>
> I am trying to sort a multi-dimentional(?) array returned from mysql (i.e.
> rows of data) based upon a calculation.
>
>
> get result from mysql
>
> sort through and calculate $distance($id) for each store, then sort the
> result according to distance ASC.
>
> list stores in order.
>
>


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@attglobal.net
==================
Nel

2005-11-29, 7:56 am

> 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@attglobal.net
> ==================


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_numUser
s,$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_s($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_s($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



Jerry Stuckle

2005-11-29, 7:56 am

Nel wrote:
>
>
> 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_numUser
s,$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_s($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_s($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@attglobal.net
==================
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com