Page 1 of 1

Rsort() Does not work properly..

Posted: Thu Sep 10, 2009 5:33 am
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

Re: Rsort() Does not work properly..

Posted: Thu Sep 10, 2009 5:55 am
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

Re: Rsort() Does not work properly..

Posted: Thu Sep 10, 2009 9:14 am
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.

Re: Rsort() Does not work properly..

Posted: Thu Sep 10, 2009 9:24 am
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.

Re: Rsort() Does not work properly..

Posted: Fri Sep 11, 2009 10:31 am
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