Reputation: 524
I have created a website ( link here ) where the breeders at the middle of the page are shown random whenever someone enters the page.
I was wondering if it would be possible that if someone goes from the homepage to another page, then presses back button, the breeder order to be the same as it was when he first entered the website.
Right now the order changes every time someone enters the homepage.
Upvotes: 0
Views: 81
Reputation: 4157
Suppose you have the breeders per string, something like
$div[0]='<div>My breeder 1</div>';
$div[1]='<div>My breeder 2</div>';
$div[2]='<div>My breeder 3</div>';
$div[3]='<div>My breeder 4</div>';
and an array
$my_breeders=[0,1,2,3];
If you shuffle the array, you will get something like
$my_breeders=[1,3,2,0];
this is the order you display your divs in. Put this in a string and write it to a cookie.
$order=implode('-',$my_breeders); //gives a string: 1-3-2-0
setcookie('breederorder',$order,time()+(30*60) )
the next time someone visits the page, check for the cookie
if(!empty($_COOKIE['breederorder'])){
$my_breeders=explode('-',$_COOKIE['breederorder']);
}
Now you have the same array as before.
Note, I set the cookie time at half an hour (30*60). If you set it too long, the next time a visitor comes to your page, het will still get the same order.
Upvotes: 1
Reputation: 492
You can save the state from the order of your elements in a cookie and prevent the randomize function, if a cookie is set.
Upvotes: 1