Back Again!


#1

I’m trying to do something simple, but I have a hard time with arrays for some stupid reason. I have a “whosonline” page that I want to sort by distance. The distance ($distc) is a function that computes after the sql query. The query returns the user, photo, etc.

After the query, I have this:

[php]<?

for($i=0;$i<$count;$i++) {
$userc=new User;
$photoc=new Photo;
$validatec=new Validate;
$userc->fillvars(mysql_fetch_array ($result,MYSQL_ASSOC));
$photoc->getprimaryphotobyuid($userc->uid,$db);
$distc=calculate_dist($user->uzip,$userc->uzip, $db);
if($distc=="–") $distc=“99999”;

?>

Then it fills the table with the results…etc.

<? } ?>[/php]

As it is, it returns the users ordered by the sql query. I would like for it to order from closest to farthest. How do I create an array(s) to sort the results?
Right now, I have it writing a temp mySQL table and filling it with the results, including the distance, then having a new query order by distance. That’s too much and I know there’s a simpler way.
Thanks for any input!
KP


#2

arrays can be manipulated in a vast majority of ways…

[phpref]array[/phpref]
it can be sorted front to back, back to front, things can be replaced, things can be removed… very similar to a tempoary row in a database… Thats how I would reference it anyways…

but if i am understanding this correctly, why can you not order it by distance when you make the original pull?


#3

I am not exactly sure on this one, but if you haven’t already I would check into this: http://www.php.net/array I believe there is a
SORT_NUMERIC() function and is used to compare items numerically.


#4

sort – Sort an array
uasort – Sort an array with a user-defined comparison function and maintain index association
uksort – Sort an array by keys using a user-defined comparison function
usort – Sort an array by values using a user-defined comparison function
arsort – Sort an array in reverse order and maintain index association
asort – Sort an array and maintain index association
array_multisort – Sort multiple or multi-dimensional arrays

Depending on how you wish to do any of these many be helpful.


#5

Thanks for your replies. I know how to use the php sorting and I can get the distance to sort from shortest to furthest, but what I need it to do is combine the distance and the user it corresponds to. In other words, I’ll have to have at least two arrays:
$distance=array($distc);
$ar=array($userc);
or one multidimensional array (one for distance and one for the users). Most of the responses here are references to php.net. I know what an array is and I know the different sort methods, I’m just not sure where to put the sorting or how to combine different arrays. If I put it in the for loop, It doesn’t sort correctly, and if I put it after the for loop, I only get one result returned. Thanks anyway, guys. I’ll figure it out, but I appreciate your responses.
Thanks again!
KP