Rotating Page Content for Testimonials etc.

September 1st, 2009 by admin Categories: Web Dev. 3 Responses
Rotating Page Content for Testimonials etc.


Bookmark and Share

Recently we helped a client solve a need by developing a featured staff section to his website.  This application allowed an administrator to upload text and an image for multiple staff members and then a randomizing script displays a different staff member every time someone visits the site.

Performing this task got me thinking about other randomizing content for a website.

Often we have a lot of great content that is, unfortunately, repetitive.  One such example would be testimonials on a web site.  Testimonials are an excellent feature of a web site that sells a product or service.  Real testimonials from real customers convey a sense of professionalism to your endeavor and go a long way towards building trust with potential new clients.

So suppose you wish to display some great testimonials in a section of your site but do not want to overdo it by displaying too many at once….  Here’s how:

  1. Create a new folder in your root directory and call it something like ‘testimonials’
  2. Open notepad and paste a testimonial into it.  If you want to have two or three testimonials appearing at a time, go ahead and use basic html formatting and paste three testimonials into this text file.
  3. Save this file as testimonials1.txt in the folder you just created.
  4. Repeat step2 and 3 for each text file necessary to get all your testimonials into a file.  Name each text file seperately (test1.txt, test2.txt, etc.)
  5. Now we will put the display code into the desired location on one of our site’s pages:
    < ?php
    $var = rand(1,3);
    switch($var) {
    case 1: include('testimonials/testimonial1.txt'); break;
    case 2: include('testimonials/testimonial2.txt'); break;
    case 3: include('testimonials/testimonial3.txt'); break;
    }
    ?>
    *note, remove the space between the initial < ? to make it execute as it should. It is shown this way to make it appear on the page rather than execute*

This snippet of code uses php to randomly display the contents of three text files that we have inside our testimonials folder. Each time the page with this code is reloaded, it randomly displays a different file.

Keep in mind that you can use any html code you want, even though it is a text file. Because it gets parsed by the php it will appear on the page as the code itself, not the file.


Bookmark and Share