Unique random every time the page is refreshed

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
dniom
Forum Newbie
Posts: 3
Joined: Mon May 10, 2010 1:41 pm

Unique random every time the page is refreshed

Post 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. :D
Any help is appreciated

Philip
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Unique random every time the page is refreshed

Post by AbraCadaver »

This works fine for showing a random array item, though I would use mt_rand(). What's not working?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
dniom
Forum Newbie
Posts: 3
Joined: Mon May 10, 2010 1:41 pm

Re: Unique random every time the page is refreshed

Post 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!
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: Unique random every time the page is refreshed

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Unique random every time the page is refreshed

Post 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']);
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
davex
Forum Contributor
Posts: 101
Joined: Sat Feb 27, 2010 4:10 pm
Location: Namibia

Re: Unique random every time the page is refreshed

Post by davex »

Nice. Much much sweeter than my bodge.

Didn't even know array_pop existed - teach my for !(RTFM).

:D

Cheers,

Dave.
dniom
Forum Newbie
Posts: 3
Joined: Mon May 10, 2010 1:41 pm

Re: Unique random every time the page is refreshed

Post by dniom »

Thanx a ton, guys!
I'll try using both variants )
Post Reply