Rsort() Does not work properly..

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
lunaticrabic
Forum Newbie
Posts: 5
Joined: Thu Sep 10, 2009 5:19 am

Rsort() Does not work properly..

Post by lunaticrabic »

So my original code is

Code: Select all

 
<?php
 
$nut=array('Bao','IM','Joel','Sacrifire','Serge','Skychin','Skylooker','Soulchild');
 
if(isset($_POST['reverse']) && $nut[0]=='Soulchild'):
rsort($nut);
 
elseif(isset($_POST['reverse']) && $nut[0]=='Bao'):
rsort($nut);
 
endif;
 
//show word
 
for($x=0;$x<8;$x++)
{
echo $nut[$x]."&nbsp;&nbsp;";
 
if ($x%2!=0)
{
echo "</br>";
}
}
 
print_r($nut);
?>
 
<form action="array.php" method="post">
<input type="submit" name="reverse" value="Reverse!!">
</form>
 
It was my homework and the task is to display an array and reverse the order of the array after the "Reverse" button is clicked.
My lecturer said the whole code should be in a one single php page.
The first click of reverse button successfully reversed the order of array but then the following does not..

Is there any problem within the php script? :(

Thanks for helping :D
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Rsort() Does not work properly..

Post by Mark Baker »

rsort() sorts the array in reverse alphabetic order. If the array is already in reverse alphabetic order, it doesn't reorder it any further , or resort it in alphabetic order
lunaticrabic
Forum Newbie
Posts: 5
Joined: Thu Sep 10, 2009 5:19 am

Re: Rsort() Does not work properly..

Post by lunaticrabic »

Mark Baker wrote:rsort() sorts the array in reverse alphabetic order. If the array is already in reverse alphabetic order, it doesn't reorder it any further , or resort it in alphabetic order
I changed the second rsort to sort but it still doesn't work...should i use array_reverse then?
Thanks for helping btw.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Rsort() Does not work properly..

Post by onion2k »

PHP doesn't remember the state (the order of the array in this case) unless you tell it to. I suggest you use either a hidden variable or sessions.
lunaticrabic
Forum Newbie
Posts: 5
Joined: Thu Sep 10, 2009 5:19 am

Re: Rsort() Does not work properly..

Post by lunaticrabic »

onion2k wrote:PHP doesn't remember the state (the order of the array in this case) unless you tell it to. I suggest you use either a hidden variable or sessions.
After seeing your comment, I went to learn session and later I figured it out.
Thanks alot onion. :D
Post Reply