Content rotation
There are many scripts which allow you to rotate banners, ad-blocks and canned paragraphs. This scripts provide you with lot of features such as statistics, split testing, cloaked links, etc. You may have a beautiful admin panel to control your campaigns…
But sometimes, all you need is rotate some content at your page. And you can implement it quick and simple without installation of a professional script.
Let’s suppose you have a main web-page where you’d like to rotate some content on.
You can create a number of files with the HTML-snippets to rotate them at your page: page1.html, page2.html, page3.html, page4.html …
page1.html: |
| <a |
page2.html: |
| <a |
page3.html: |
| <a |
page4.html: |
| <a |
Content rotation with a PHP script
To be the simplest make your main web-page as a PHP-page (index.php).
At the first, you have to create the list of your rotating files:
<?php
$content_rotation_list = array(
"page1.html",
"page2.html",
"page3.html",
"page4.html"
);
?>
Then include the content-rotation PHP-script:
<? include("content-rotation.php"); ?>
You can download the content-rotation.php here: content-rotation-php.zip
Note. Please UnZip the content-rotation-php.zip file before uploading to your site.
However the script is fairly simple:
content-rotation.php: |
<?php |
Take a look at the sample of using the script below:
index.php: |
<html> |
Content rotation with a JavaScript
Sometime you’re not able or don’t want to use a PHP. In this cases you can use a JavaScript to get the same result with your html-pages.
At the first, you have to create the list of your rotating files:
<script type="text/javascript">
<!--
content_rotation_list = [
"page1.html",
"page2.html",
"page3.html",
"page4.html"
];
//-->
</script>
Then include the content-rotation JavaScript:
<script type="text/javascript" src="content-rotation.js">
</script>
The content-rotation.js script is more complicated then the php-version of the rotation script. You can download it here: content-rotation-js.zip
Note. Please UnZip the content-rotation-js.zip file before uploading to your site.
Take a look at the sample of using the script below:
index.html: |
<html> |







February 12th, 2007 at 22:44
Hi Michel,
What script works for the most users because many still use the older versions of explorer and netscape:
PHP script
or
JavaScript?
What would you recommend to use?
Thanks in advance,
Richard
February 13th, 2007 at 05:49
Hello Richard,
If you have PHP-support at your site and use php web-pages,
the PHP-version is better choice.
You’ll need the JavaScript version where you can’t use the PHP one.
I hope this helps.
Michel
June 13th, 2007 at 09:04
Jaki Degg…
I Googled for something completely different, but found your page…and have to say thanks. nice read….
May 27th, 2008 at 22:55
Hi Michel,
This by far is the simplest and easiest script to implement and blows away anything else out there. Now we would like to have a link or button sequentially rotate these pages instead of randomizing. I’ve been looking for sequential php arrays, but not sure how to implement. Seems relatively simple for this. Any pointers?
May 28th, 2008 at 05:18
Hi Garrett,
You can adjust the content-rotation.php script to reload your page with additional parameter and show the next content-file.
Take a look at this code
content-rotation.php:<?phpif (isset($content_rotation_list)
&& is_array($content_rotation_list)
&& count($content_rotation_list) > 0) {
$next = isset($_GET["next"])? intval($_GET["next"]): 0;
include($content_rotation_list[$next]);
if (++$next >= count($content_rotation_list)) $next = 0;
print "<a href="?next=$next">next</a>"
}
?>
Hope this helps.
Michel
May 28th, 2008 at 05:31
Use different quota marks with the print PHP-operator:
print "<a href='?next=$next'>next</a>"
May 28th, 2008 at 18:02
I’m was getting an unexpected parsing error, but it just needed a “;” at the end of the print statement.
Now I’m trying to position the print statement in so it appears in more strategic places on the page.
Thanks for the response. Good stuff.
Garrett
May 28th, 2008 at 18:16
Actually, I’m trying to get the print statement or link above the content that outputs. Any ideas? I’ve switched things around and it still prints below.
Garrett
May 28th, 2008 at 18:58
I actually figured it out, just move the include statement below the print statement. I’m learning!
print “Next Event“;
include($content_rotation_list[$next]);
Thanks again!
May 29th, 2008 at 04:37
Hello Garrett,
When you place the
print-statement above theincludeyou need to change the code like this:<?phpif (isset($content_rotation_list)
&& is_array($content_rotation_list)
&& count($content_rotation_list) > 0) {
$next = isset($_GET["next"])? intval($_GET["next"]): 0;
if ($next >= count($content_rotation_list)) $next = 0;
print "<a href='?next=" . ($next + 1) . "'>next</a>";
include($content_rotation_list[$next]);
}
?>
May 30th, 2008 at 10:23
hi guys,
i need the rotation to be automatic after X seconds, as opposed to having a button to reload, what would the code be like?
May 30th, 2008 at 10:59
Just add
<meta>tag into the head of your web-page to reload the page every X seconds.The code below automatically reloads the page every 15 seconds:
<html><head>
<meta http-equiv="refresh" content="15">
</head>
<body>
. . . Content of your page . . .
June 3rd, 2008 at 09:45
Hi Michel ,
Thanks for the script .
How can i pull values from database for rotation ?
i want to rotate html code ,
how can i rotate html code after specified time ( 2 hours etc .) ?
Please reply .
June 6th, 2008 at 07:48
Hi Koolajay,
When you use a database with server scripts
you need to select your content by some criteria.
This absolutely depends on your database structure/data.
You can use system timer to toggle the selection criteria by specified time.
For example, the
time()PHP-function gets currenttimestamp(the number of seconds since January 1, 1970). So thetime() / 3600;sentence produces the number of hours since January 1, 1970. Andtime() / 7200;provides you with a unique number every 2 hours.Lets suppose you have 17 database records with html-code to rotate them on your page. And each of these records has an ordering number. So the PHP code-snippet below could select a new record every 2 hours:
<?php. . .
$timeFrame = 2; // 2 hours to rotate
$numberOfRecords = 17;
$selectNumber = (time() / (3600 * $timeFrame)) % $numberOfRecords +1;
$sqlQuery = "SELECT code FROM table WHERE num=$selectNumber";
if ($sqlResult = mysql_query($sqlQuery)) {
if ($rec = mysql_fetch_assoc($sqlResult)) {
print $rec["code"]
}
}
. . .
?>
Hope this helps.
Michel
September 2nd, 2008 at 00:40
Michael,
I’m very interested in your content rotation script - I really need to make it work.
Based on your example, it seems each of my rotating content blocks must be placed on a separate .PHP page - is that correct? If this is true, then it seems logical to assume that I’ll have to rotate each of the content blocks into a frame - this is not ideal.
So here’s my question: Do I need a to place each content block on a separate page? I just need to accomplish either of two things: 1) either rotate each block each time a visitor revisits or refreshes their browser, or 2) auto-rotate each block on, say, 2-hour intervals.
Are my assumptions correct?
Please advise at your earliest opportunity.
Thanks much.
Doug C.
October 9th, 2008 at 15:58
Michael,
What I am trying to do is a “tip of the day.” As I see it I can do this without a database by calling a php script to pull up the html content (and use seperate html files for each tip,) and (in the same script??) use the sequential array modifications you show above so they don’t come up randomly, plus (in the same script??) use time() fuction to rotate the html files daily. I would also like to place this code in its own file and call in from my home page. Is all that possible, or an I going down the wrong road?
Stephen
October 21st, 2008 at 12:54
Hi Stephen,
Yes, this is possible.
The original script is designed to show HTML-snippets in random order.
In order to show one “tip” per day you can use the php-function date()
to get the number of a day in the year:
date("z")So the php-script would look like this:
<?php$content_rotation_list = array(
"page1.html",
"page2.html",
"page3.html",
"page4.html",
. . .
);
$tip_idx = date("z") % count($content_rotation_list);
include( $content_rotation_list[ $tip_idx ] );
?>
Hope this help.
Michel
October 28th, 2008 at 16:26
Hi Michel. I’m trying to do something similar to Mistephen - content rotation based on the day/week/month of the year. I can do it using ssi files, but they don’t cooperate well with php pages.
I’ve uploaded your index.php, content-rotation.php and some dummy index.html pages to my server, but I can’t see how it’s choosing the specific filenames based on the date command.
Is there a way to specify that it looks for, say, 1.shtml, 2.html and so on, if that number is the day of the year? If so, would you be able to publish the entire code that I’ll need? (obviously the 1.html or 1.jpg etc files are up to me to create!)
Many thanks in advance. I’ve spent hours looking for a solution and am more confused than ever!
October 28th, 2008 at 16:56
Hi Joe,
Lets suppose you have 365 files named as
1.html,2.html, …,180.html, …,365.html. One file per a day of the year. Then you don’t need thiscontent-rotation.phporindex.phpfiles.You can use this simple php-snippet below to include your numbered html-files into any of your php-pages:
<?phpinclude ( date("z") . ".html" );
?>
Michel
October 29th, 2008 at 12:36
Ah, so simple, yet so elusive. Thanks so much Michel! I’m really grateful for your help with this - it’s solved a lot of problems for me.
I’m pretty much a php newbie, with my knowledge limited to hacking around with WordPress themes. I appreciate you sharing your knowledge with others.
Joe
July 3rd, 2009 at 21:31
Awesome script, just what I needed. Thanks a lot!
September 30th, 2009 at 12:47
Hello,
Thanks for your nice script, it was exactly what I was looking for.
I have just one question regarding the Java method:
Would it be possible to have the script either another page daily or upon page reload?
And if so, how would I implement this?
Thanks.
October 5th, 2009 at 06:20
The
content-rotation.jsscript has code:if (content_rotation_list.length > 0) {var url = content_rotation_list[
Math.floor(Math.random()*(content_rotation_list.length-1))
];
This code gets the URL from content list in random order.
You can change this code to show anoher page daily:
if (content_rotation_list.length > 0) {var day = parseInt((new Date).valueOf()/86400000;
var url = content_rotation_list[
day%content_rotation_list.length)
];
If you’d like change your page on reload you need to play with cookies.
Hope this helps.
Michel
November 28th, 2012 at 17:08
Michel,
“You said: Just add tag into the head of your web-page to reload the page every X seconds.
The code below automatically reloads the page every 15 seconds:”
But the way my CMS works, I can’t get to the megatags. Any other way to get the content the refresh after a certain amount of time” I’ve seen other scripts where it was included in the script.
November 28th, 2012 at 18:12
Hi ddennis,
You can use a Javascript like this below to reload a page every 15 seconds:
<script type="text/javascript">
setTimeout("window.location.reload(true)", 150000);
</script>
Hope this helps.
Michel
December 13th, 2012 at 18:38
Michael,
First, thank-you. This thread has been valuable and educational for me, a PHP newbie.
The sequential rotation PHP code is mostly what I need, and I have it working correctly on my site with the href link.
How can I make it rotate content on a timed interval rather than have the user click the link? I know I can construct a while(true){ loop, and use a sleep(10), but the rest is a mystery to me. How to I invoke the “?next=$next” ?
December 14th, 2012 at 09:22
Hi Russcus,
You can’t just use the sleep(10) inside PHP code and wait it would pause your page. It pauses the running PHP script, not the page. The page appears in your browser after your PHP script has fully completed its work.
In any case you need to refresh your page to rotate the content.
You either use a link clicking or add a refresh code to do this automatically.
You coud use a javascript to refresh the page with the “?next=X” parameter.
You can generate this javascript code with your PHP script like this:
—-
<?php
if (isset($content_rotation_list)
&& is_array($content_rotation_list)
&& count($content_rotation_list) > 0) {
$next = isset($_GET["next"])? intval($_GET["next"]): 0;
include($content_rotation_list[$next]);
if (++$next >= count($content_rotation_list)) $next = 0;
}
?>
<script type="text/javascript">
setTimeout( 'window.location.href = window.location.href.split("?")[0] + "?next=<?php echo $next; ?>" ', 15000);
</script>
—-
December 15th, 2012 at 01:53
Michael,
Thanks for the suggestions and the fast reply.
I have the page refreshing on a ten-second cycle with this echo suggested by someone else:
echo “”;
The page in question is at
The request I received today is this:
Rather than have all the content rotated, can we rotate just the headline and sub-head and place a link directly below them that inserts the paragraph that presently appears between the sub-head and the “Ask us how” link?
The only things I can come up with is
1. link to a unique static page for each unique set of headline+content (three pages, not a big deal)
2. Change your print “next“; line so that when the link is clicked, the current content is replaced with the different content from a different file, the page is refreshed, and the timed refreshing is halted.
Seems like a tall order, and I’m not nearly competent enough with PHP to begin to figure it out.
December 15th, 2012 at 01:57
Well, I see the page stripped out my code. Not sure how to delimit it so you can read it.
Sorry.