Randomising Advertisments

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
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Randomising Advertisments

Post 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
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Store them in an array and play around with array_rand()

http://us4.php.net/manual/en/function.array-rand.php
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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>';
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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?
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Code: Select all

echo '<HR><PRE>'; print_r($input); echo '</PRE>';
Returns:

Code: Select all

Array
(
    &#1111;0] =&gt; Neo
    &#1111;1] =&gt; Morpheus
    &#1111;2] =&gt; Trinity
    &#1111;3] =&gt; Cypher
    &#1111;4] =&gt; Tank
)

Code: Select all

echo '<HR><PRE>'; print_r($rand_keys); echo '</PRE>';
Returns:

Code: Select all

Array
(
    &#1111;0] =&gt; 0
    &#1111;1] =&gt; 4
    &#1111;2] =&gt; 2
    &#1111;3] =&gt; 1
    &#1111;4] =&gt; 3
)
Take a look at the values of the second array. They are mixed. Those are the array keys of the first array.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

I'm not sure man. I tested it with the stuff I posted above and it worked fine for me.
User avatar
Mr Tech
Forum Contributor
Posts: 424
Joined: Tue Aug 10, 2004 3:08 am

Post 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 :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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).
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post 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 :)
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Code: Select all

$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
shuffle($input);
foreach ($input as $name) print($name."\n");
shuffle() does wonders. ;)
Post Reply