Random Banners

In my recent re-design on Movalog, I had three banners and unfortunately I liked them all and couldn't figure out which one I liked best. As a result, I decided to use them all.

First I moved all the images to a subfolder called banner and renamed the images to 1.jpg, 2.jpg and 3.jpg. Finally I used PHP's mt_rand function to randomly print a number from 1 to 3 (mt_rand works better than the plain rand function function), like so

<?php echo mt_rand(1,3); ?>

Hence my banner image code looks like this

<?php echo mt_rand(1,3); ?>
<img src="http://www.movalog.com/images/banner/<?php echo mt_rand(1,3); ?>.jpg" alt="Movalog - All Things Movable Type" />

7 Comments

salguod said:
on Jun 23, 2005 5:34 PM | Reply

That's cool, but every time I open your site I see just the green banner with the open end wrench on it.

FYI - On my PC, the line of code spills outside your text area so that I can't see the end of the code line. I'm at work using IE 6 on Win 2K with the latest updates.

Arvind Satyanarayan said:
on Jun 23, 2005 5:51 PM | Reply

I have a feeling that your web browser is picking up the cache from the old old stylesheet. Try emptying the cache and trying again. Also I advice you to try another browser like Firefox, Movalog looks very slick in Firefox!

Another thing to note is that the banner doesn't change on every refresh, it may take quite a few refreshes to change.

elise said:
on Jun 23, 2005 9:47 PM | Reply

Hey Arvind, That's pretty cool. I have found though that the rand() function doesn't work very well. Scriptygoddess has posted a fix here that works much better.

salguod said:
on Jun 23, 2005 10:18 PM | Reply

OK, now it's working. I didn't realize that they're all green with a wrench. Didn't one of them used to be blue?

I have FF at home, but I actually like Maxthon better, even though it's IE based. Tons of great functionality built in. I learned about it from this article from the Windows Support Center .

Here at work I have no choice, but the IT guy has talked about switching us to FF. I kinda doubt it will happen, though.

Arvind Satyanarayan said:
on Jun 23, 2005 11:25 PM | Reply

Thanks for the heads up Elise, I've updated the tutorial to use the mt_rand() function as opposed to rand()

Juan Mesa said:
on Mar 18, 2007 8:14 AM | Reply

I have tried this but still not working I have change things to php extension, and everything. I don't know if I have to create a php file and put it in my server. Thanks for helping me.

Kez6 said:
on Dec 12, 2007 4:05 PM | Reply

How to random Pictures and Links ?

Leave a comment

Preview