Page 1 of 1
hey, jason, question for you
Posted: Wed May 29, 2002 8:51 pm
by gotDNS
hey, jason, how could I have something be sortable by (...oh say) natural numerical order [natsort()] and in descending order? I noticed you did this on the member list, so thanks.
Posted: Wed May 29, 2002 10:05 pm
by hob_goblin
i know this wasn't directed at me but you might want to look into
array_reverse()
http://www.php.net/array_reverse
you could sort them, put them in an array and then reverse them..
Posted: Thu May 30, 2002 6:57 am
by gotDNS
thanks, hob_gobblin
Posted: Thu May 30, 2002 7:32 am
by gotDNS
hmm, i don't really understand that. I was hoping to do it with if/switch statements....any other ideas? Unless u can explain the reverse function
Posted: Thu May 30, 2002 7:45 am
by twigletmac
Using array_reverse - if the array to be sorted was $array:
Code: Select all
<?php
// Sort in ascending order
$array_up = natsort($array);
// Sort in descending order
$array_down = array_reverse(natsort($array));
?>
All that array_reverse does is reverse the order of the elements in the array.
Mac
Posted: Thu May 30, 2002 2:02 pm
by gotDNS
see, i need to make an array out of image widths of 250 (some odd) files.
I have it display the width x height for each, and i want them to be sortable by size and also have a sereparte menu for each that says ASC or DESC order. Thanks!
Posted: Thu May 30, 2002 2:52 pm
by hob_goblin
you could have some links at the bottom like..
Code: Select all
<a href="<?=$PHP_SELF;?>?op=asc">Ascending!</a><br />
<a href="<?=$PHP_SELF;?>?op=des">Descending!</a><br />
and assuming the results were $array ... you could have
Code: Select all
<?php
if($op == "asc"){
$sorted_array = natsort($array);
}
if($op == "des"){
$sorted_array = array_reverse(natsort($array));
} else {
$sorted_array = natsort($array);
//making ascending default..
}
$a_c = count($sorted_array);
echo "<table><tr><td><b>Widths</b></td></tr>
for($i = 0; $i <= $a_c; $i++){
echo "<tr><td>".$sorted_arrayї$i]."</td></tr>\n"
}
?>
to descide whether to ascend or descend...