Page 1 of 1
Unique random every time the page is refreshed
Posted: Mon May 10, 2010 1:49 pm
by dniom
Hi. Can anyone help me please?
Is there any way to show a unique random item from an array,
every time you refresh the page?
In other words how do I show the items from an array
in random order, one by one, by reloading the page?
Moreover the page have to be refreshed by pressing the "submit" button...
Thats what I achieved so far
but it doesn't display the UNIQUE array item every time the page is reloaded...
as you may guess )
Code: Select all
<html>
<head><title></title></head>
<body>
<?php
$testArray = array ("one","two","three","four","five");
$number = rand(0,4);
echo "$testArray[$number]";
?>
<form method="post" action="test.php">
<input type="submit" value="Submit">
</form>
</body>
</html>
I'm a designer/ActionScript sort of a guy and know nothing about php, so please don't laugh.
Any help is appreciated
Philip
Re: Unique random every time the page is refreshed
Posted: Mon May 10, 2010 2:14 pm
by AbraCadaver
This works fine for showing a random array item, though I would use mt_rand(). What's not working?
Re: Unique random every time the page is refreshed
Posted: Mon May 10, 2010 4:09 pm
by dniom
AbraCadaver wrote:This works fine for showing a random array item, though I would use mt_rand(). What's not working?
Everything works, but not actually the way I wanted it to.
The thing is that I want each item from an array to appear only once.
Until of course there are no more items in the array left to retrieve ---
then the whole process can start all over again.
Thanks for the reply!
Re: Unique random every time the page is refreshed
Posted: Mon May 10, 2010 4:15 pm
by davex
Hi,
Ok well I think what you would have to do is not only generate a random number but also to persist which elements had already been displayed so they are not displayed next time.
There are a couple of different ways you can do this and which is best really depends on how "secure" you need it to be (can a user mess with input) and the size of the arrays (is it more effecient to store a list of keys or to re-order the array randomly somehow first etc).
A simple bit of code (disclaimer: haven't actually tried this so bound to be syntax errors at the least and also I've done it in a really step-by-step simple way to be clear if not very inefficient and cack) you could do something like:
page.php:
Code: Select all
<?php
$my_array = array("one","two","three","four");
$count = count($my_array);
if (isset($_REQUEST['already_seen'])) $already=explode(":",$_REQUEST['already_seen']);
else $already=array();
if (count($already) >= $count)
{
echo "You have seen them all now!";
exit();
}
mt_srand(microtime()*1000000);
$found_one=false;
while (!$found_one)
{
$one = mt_rand(0,$count-1);
if (!in_array($already[$one])) $found_one=true;
}
echo $my_array[$one];
$already[]=$one;
echo "<br />";
echo "<a href=page.php?already_seen=".urlencode(implode(":",$already)).">Click here</a>";
?>
HTH,
Cheers,
Dave.
Re: Unique random every time the page is refreshed
Posted: Mon May 10, 2010 4:26 pm
by AbraCadaver
I'd probably do it like this:
Code: Select all
session_start();
if(!isset($_SESSION['testArray']) || empty($_SESSION['testArray'])) {
$_SESSION['testArray'] = array("one","two","three","four","five");
shuffle($_SESSION['testArray']);
}
echo array_pop($_SESSION['testArray']);
Re: Unique random every time the page is refreshed
Posted: Mon May 10, 2010 4:34 pm
by davex
Nice. Much much sweeter than my bodge.
Didn't even know array_pop existed - teach my for !(RTFM).
Cheers,
Dave.
Re: Unique random every time the page is refreshed
Posted: Mon May 10, 2010 5:34 pm
by dniom
Thanx a ton, guys!
I'll try using both variants )