Creating Randomly Rotating PHP Script

This tutorial covers how to create a script that randomly pulls in values and displayed them on a page of your website. This is very useful if you want to make a section on your site that cycles through “helpful tips” or “customer testimonials”. It can be used for anything and really is a very simple code. Follow the steps below:

1.) Open up Dreamweaver and open up a new blank PHP page.
2.) Insert the following code:


$bannerCounter= “1″;

$bannerCode[$bannerCounter] = “The technician was early! On a scale of 1 to 5, technician was a 5+ – Mr. Burkart of Chesterfield”;

$bannerCounter++;

$bannerAdTotals = $bannerCounter – 1;
if($bannerAdTotals>1)
{
mt_srand((double)microtime() * 1234567);
$bannerPicked = mt_rand(1, $bannerAdTotals);
}
else
{
$bannerPicked = 1;
}
$bannerAd = $bannerCode[$bannerPicked];

3.) Save this page as testimonials.php

4.) Open up the page you want the random text to be displayed on and input the following code:

include_once(“testimonials.php”);
echo “$bannerAd”;

5.) Upload both files to your server and enjoy your new randomly rotating text

*Note to add additional text to the file to be randomly displayed simply copy the following lines and add in the new text:


$bannerCounter++;

$bannerCode[$bannerCounter] = “Add in the new text inside of these quotes”;

Hopefully you find this tutorial helpful, and as always if you have any questions feel free to drop us a comment below and a member of our team will get back to you.

Leave a reply

Close