Page 1 of 1
Randomising Advertisments
Posted: Mon May 02, 2005 10:50 pm
by Mr Tech
Hi there,
I have about five advertisements on my website which are lsited down the right hand side of my website, one below the other.
What I'm wanting to do is randomise they order in which they display in. How would I do this?
Thanks
Ben
Posted: Mon May 02, 2005 11:10 pm
by hawleyjr
Store them in an array and play around with array_rand()
http://us4.php.net/manual/en/function.array-rand.php
Posted: Mon May 02, 2005 11:20 pm
by Mr Tech
Thanks Hawleyjr.
I tried the example code but it only showed two at a time so I modified the code to this:
Code: Select all
<?php
srand((float) microtime() * 10000000);
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 5);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
echo $input[$rand_keys[2]] . "\n";
echo $input[$rand_keys[3]] . "\n";
echo $input[$rand_keys[4]] . "\n";
?>
But it no longer changes... Any ideas why it's not working?
Thanks
Ben
Posted: Mon May 02, 2005 11:22 pm
by hawleyjr
Use the following code to print your arrays. It does wonders
Code: Select all
echo '<HR><PRE>'; print_r($input); echo '</PRE>';
echo '<HR><PRE>'; print_r($rand_keys); echo '</PRE>';
Posted: Mon May 02, 2005 11:34 pm
by Mr Tech
Don't know if it is just me but it still doesn't seem to change evrytime... Maybe every 20 refreshes... ANy ideas why this isn't always changing?
Posted: Mon May 02, 2005 11:39 pm
by hawleyjr
Code: Select all
echo '<HR><PRE>'; print_r($input); echo '</PRE>';
Returns:
Code: Select all
Array
(
ї0] => Neo
ї1] => Morpheus
ї2] => Trinity
ї3] => Cypher
ї4] => Tank
)
Code: Select all
echo '<HR><PRE>'; print_r($rand_keys); echo '</PRE>';
Returns:
Code: Select all
Array
(
ї0] => 0
ї1] => 4
ї2] => 2
ї3] => 1
ї4] => 3
)
Take a look at the values of the second array. They are mixed. Those are the array keys of the first array.
Posted: Mon May 02, 2005 11:50 pm
by Mr Tech
It seems to stay on order of numbers and then change to something different after anywhere after 20 refreshes and then changes back again to this...
Code: Select all
Array
(
[0] => 1
[1] => 0
[2] => 3
[3] => 4
[4] => 2
)
Sorry if I'm not getting the point but it just seems weird that it isnt changing... I want it to change everytime, or nearly everytime they refresh/visit another page...
Ben
Posted: Mon May 02, 2005 11:53 pm
by hawleyjr
I'm not sure man. I tested it with the stuff I posted above and it worked fine for me.
Posted: Mon May 02, 2005 11:56 pm
by Mr Tech
I've been testing it on my local server. I tried uploading it and it seems to now work... Maybe the version on my local server is too old for it?
Thanks for the help though

Posted: Tue May 03, 2005 2:32 pm
by Ambush Commander
There's something wrong with the randomness generator: your random pool doesn't have enough entropy (aka it has a tendency to regenerate the same thing).
Posted: Tue May 03, 2005 3:38 pm
by hawleyjr
Ambush Commander wrote:There's something wrong with the randomness generator: your random pool doesn't have enough entropy (aka it has a tendency to regenerate the same thing).
1 in 120 if I did my math right

Posted: Tue May 03, 2005 9:11 pm
by Skara
Code: Select all
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
shuffle($input);
foreach ($input as $name) print($name."\n");
shuffle() does wonders.
