hey, jason, question for you

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

hey, jason, question for you

Post 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.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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..
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

thanks, hob_gobblin
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post 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!
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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&#1111;$i]."</td></tr>\n"
}


?>
to descide whether to ascend or descend...
Post Reply