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

23 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

Got a question?   Leave a Reply

You must be logged in to post a comment.