How to make a simple rotating banner script in PHP

Need a simple rotating Banner? Lets make one right now!

Hello and welcome back to JW's PC Tips today I'll be explaining and demonstrating exactly how to make a rotating PHP script.If you don't know PHP then no worries, I'll try and make this so simple anyone can do it! yay!

Prerequisites:

PHP enabled website

Now that you have your website ready, lets add the rotating PHP script. (Don't get overwhelmed by this.)

 

<?php
$variable[1] = 'First rotating text';
$variable[2] = 'Second rotating text';
$variable[3] = 'Third rotating text';
$variable[4] = 'Fourth rotating text';
$variable[5] = 'Fifth Rotating text';

$adCount = count($variable);
$randomAdNumber = mt_rand(1, $adCount);
echo $variable[$randomAdNumber];
?>

As the code is now you can plug it into your website anywhere between the<body> </body> tags and it will rotate through all the lines of text when the page is refreshed.

 Now lets explain this code line by line for you.

1. Begins the PHP script. <?php
2. Stores "First rotating text" inside variable[1].
$variable[1] = 'First rotating text';
3. Stores "Second rotating text" inside variable[2].
$variable[2] = 'Second rotating text';
4. Stores "Third rotating text" inside variable[3].
$variable[3] = 'Third rotating text';
5. Stores "Fourth rotating text" inside variable[4].
$variable[4] = 'Fourth rotating text';
6. Stores "Fifth rotating text" inside variable[5].
$variable[5] = 'Fifth rotating text';
7. Just an empty line  
8. Prepares all 5 lines of text for execution.
$adCount = count($variable);
9. picks a random line of text to display.
$randomAdNumber = mt_rand(1, $adCount);
10. Displays the randomly selected line of text.
echo $variable[$randomAdNumber];
11. Closes the PHP script  ?>

 

So to keep things painfully easy all you have to do to customize this PHP script is to change the text in the code "First rotating text " through "Fifth rotating text" to whatever you want rotated (i.e images, text, media).


This is about as simple as it gets when making a rotating PHP script but if you did not understand this demonstration then feel free to submit a question or comment in the comment box below.


hi

visitor's picture

hi

Hi, thanks for the great

visitor's picture

Hi,

thanks for the great tip, it works like a charm! I was wondering, Is there a way we can use this to display more than 1 item at a time? For example, if I had 20 variables and wanted to show 3 at a time? Thanks

I tried this and it worked,

visitor's picture

I tried this and it worked, but when I tried to add it to my footer the footer didn't even show up. There are no tags and such in this footer. I tried adding it but it didn't make no difference

Wow! You really did it! I

samm's picture

Wow! You really did it! I learn something very interesting today, I think I am gonna stick around for a while. Thanks for sharing the tips!
Samm, display fasteners

how do you change the var to

visitor's picture

how do you change the var to an image?

Change $variable[1] = 'First

JW's picture

Change  $variable[1] = 'First rotating text';  To $variable[1] = '<img src="http://whatever.com/image.jpg" alt="" />'; 

Question

visitor's picture

Please, how would I write this PHP if I just wanted to have the header randomly cycle through a folder of images?Thank you! 

Post new comment

  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
Are you a human?