Randomising Advertisments
Moderator: General Moderators
Randomising Advertisments
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
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
Store them in an array and play around with array_rand()
http://us4.php.net/manual/en/function.array-rand.php
http://us4.php.net/manual/en/function.array-rand.php
Thanks Hawleyjr.
I tried the example code but it only showed two at a time so I modified the code to this:
But it no longer changes... Any ideas why it's not working?
Thanks
Ben
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";
?>Thanks
Ben
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>';Code: Select all
echo '<HR><PRE>'; print_r($input); echo '</PRE>';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>';Code: Select all
Array
(
ї0] => 0
ї1] => 4
ї2] => 2
ї3] => 1
ї4] => 3
)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...
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
Code: Select all
Array
(
[0] => 1
[1] => 0
[2] => 3
[3] => 4
[4] => 2
)Ben
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Code: Select all
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
shuffle($input);
foreach ($input as $name) print($name."\n");