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 href="http://digg.com/submit?phase=2&url=http://askmichel.icoder.com/2006/10/30/content-rotation/ &title=Content+rotation"> <img border="0" src="/wp-content/plugins/sociable/images/digg.png" width="80" height="70" /></a><br />Digg.com
page2.html:
<a href="http://del.icio.us/post?url=http://askmichel.icoder.com/2006/10/30/content-rotation/ &title=Content+rotation"> <img border="0" src="/wp-content/plugins/sociable/images/delicious.png" width="80" height="80" /></a><br />del.icio.us
page3.html:
<a href="http://reddit.com/submit?url=http://askmichel.icoder.com/2006/10/30/content-rotation/ &title=Content+rotation"> <img border="0" src="/wp-content/plugins/sociable/images/reddit.png" width="90" height="90" /></a><br />reddit.com
page4.html:
<a href="http://co.mments.com/track?url=http://askmichel.icoder.com/2006/10/30/content-rotation/ &title=Content+rotation"> <img border="0" src="/wp-content/plugins/sociable/images/co.mments.gif" width="80" height="80" /></a><br />co.mments.com

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
 if (isset($content_rotation_list)
 && is_array($content_rotation_list)
 && count($content_rotation_list) > 0) {
  srand(time());
  include($content_rotation_list[
   rand(0,count($content_rotation_list)-1)]);
 }
?>

Take a look at the sample of using the script below:

index.php:
<html>
<head>
 <title>Sample of the PHP rotation script</title>
</head>
<body>
<h3>
<button onclick="location.reload(true)"> Refresh </button>
Sample of the PHP rotation script</h3>
<?php
 $content_rotation_list = array(
  "page1.html",
  "page2.html",
  "page3.html",
  "page4.html"
 );

?>
<? include("content-rotation.php"); ?>
</body>
</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>
<head>
 <title>Sample of the JavaScript rotation script</title>
</head>
<body>
<h3>
<button onclick="location.reload(true)"> Refresh </button>
Sample of the JavaScript rotation script</h3>
<script type="text/javascript">
<!--
content_rotation_list = [
  "page1.html",
  "page2.html",
  "page3.html",
  "page4.html"
];

//-->
</script>
<script type="text/javascript" src="content-rotation.js">
</script>

</body>
</html>

 

by Michel Komarov, © Copyright 2006. iCoder.com

 

Share in social bookmarking:These icons link to social bookmarking sites where readers can share and discover new web pages.  digg del.icio.us BlinkList Reddit NewsVine YahooMyWeb co.mments

34 Responses to “Content rotation”

  1. Richard Says:

    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

  2. michel Says:

    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

  3. Jaki Degg Says:

    Jaki Degg…

    I Googled for something completely different, but found your page…and have to say thanks. nice read….

  4. BumpStop_com Says:

    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?

  5. michel Says:

    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:

    <?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;
     print "<a href="?next=$next">next</a>"
    }
    ?>

    Hope this helps.
    Michel

  6. michel Says:

    Use different quota marks with the print PHP-operator:
    print "<a href='?next=$next'>next</a>"

  7. BumpStop_com Says:

    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

  8. BumpStop_com Says:

    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

  9. BumpStop_com Says:

    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!

  10. michel Says:

    Hello Garrett,

    When you place the print-statement above the include you need to change the code 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;
     if ($next >= count($content_rotation_list)) $next = 0;
     print "<a href='?next=" . ($next + 1) . "'>next</a>";
     include($content_rotation_list[$next]);
    }
    ?>
  11. ekwamba Says:

    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?

  12. michel Says:

    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 . . .
  13. koolajay Says:

    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 .

  14. michel Says:

    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 current timestamp (the number of seconds since January 1, 1970). So the time() / 3600; sentence produces the number of hours since January 1, 1970. And time() / 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

  15. dcarlson007 Says:

    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.

  16. mistephen Says:

    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

  17. michel Says:

    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

  18. gorky5 Says:

    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!

  19. michel Says:

    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 this content-rotation.php or index.php files.

    You can use this simple php-snippet below to include your numbered html-files into any of your php-pages:

    <?php
    include ( date("z") . ".html" );
    ?>

    Michel

  20. gorky5 Says:

    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

  21. Nick Says:

    Awesome script, just what I needed. Thanks a lot!

  22. vvdven Says:

    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.

  23. michel Says:

    The content-rotation.js script 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

  24. ddennis Says:

    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.

  25. michel Says:

    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

  26. russcus Says:

    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” ?

  27. michel Says:

    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>
    —-

  28. russcus Says:

    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.

  29. russcus Says:

    Well, I see the page stripped out my code. Not sure how to delimit it so you can read it.
    Sorry.

  30. gbengalex Says:

    Michel I have a slightly different question. I want to rotate this cartoon URL every day http://www.sitename.com/add_toon_info.php?date=20130702, where “20130702″ is Year (2013) Month (07) and Day (02). So the idea is to have a php script that will change the value “20130702″ everyday on my webpage.

  31. michel Says:

    Hello gbengalex,

    You can use a simple PHP script to redirect to the URL or to load the content from the URL by the date like this:

    <?php
    $date = date("Ymd");
    //use for redirection
    header("Location: http://www.sitename.com/add_toon_info.php?date=" . $date);
    //use for loading content
    echo file_get_contents("http://www.sitename.com/add_toon_info.php?date=" . $date);
    ?>
  32. gbengalex Says:

    Thanks Michel! Works like a charm. Assuming that I want the content date to start rotating from the past - say 19980220 (where year is 1998, month is 02 and day is 20), how can i tweak the code please? Right now it is loading current dates but i want it to fetch from archives starting from 1998. Cheers

  33. michel Says:

    Hi gbengalex,

    If you want to start from 1998 you have to shift the date by -15 years (2013 - 1998).
    If you also want to start from a month different from the current one. you have to shift the month as well. For example, use -5 months now to starts from February.

    <?php
    $datemod = strtotime("-15 years -5 months");
    $date = date("Ymd", $datemod);
    ...

    Hope this helps.

  34. gbengalex Says:

    Thank you!

Got a question?   Leave a Reply

You must be logged in to post a comment.