scriptingBlog.com - For those who work online
Do you have something interesting to share with others? If you do, click here to write a post or a press-release to the blog. All 100% free!

Simple Ad Rotator

This is a simple function that can be used to create ads that rotate according to a random order. Easy to setup and customize. Just define your ads and set the number of ads displayed at once on your page. No database is required.
See a live example!

Haw to use?

To define a new ad just insert a new line like this:
$ad[] = array(“Ad title “, “Ad description “, “Display URL”, “URL”);

To display the ads include the file “ad_rotator.php” and after that call the functions show_ads().
Example:

<?php

include (”/path/to/ad_rotator.php“);

show_ads(3); // this will display 3 ads

show_ads(1); // this will display 1 ad

?>

Source code:


<?php
/*************************************************
 * Simple AD Rotator
 *
 * Version: 1.0
 * Date: 2007-08-30
 *
 * Include this php file into your site and call the
 * show_ads() function with a number of ads
 * you want to display.
 *
 * See example.php
 *
 ****************************************************/

//USAGE $ad[] = array("Title", "Description", "display url", "url");

$ad[] = array("Title 0""Some description for Ad 0""www.google.com""http://www.google.com");
$ad[] = array("Title 1""Some description for Ad 1""www.google.com""http://www.google.com");
$ad[] = array("Title 2""Some description for Ad 2""www.google.com""http://www.google.com");
$ad[] = array("Title 3""Some description for Ad 3""www.google.com""http://www.google.com");
$ad[] = array("Title 4""Some description for Ad 4""www.google.com""http://www.google.com");
$ad[] = array("Title 5""Some description for Ad 5""www.google.com""http://www.google.com");
$ad[] = array("Title 6""Some description for Ad 6""www.google.com""http://www.google.com");
$ad[] = array("Title 7""Some description for Ad 7""www.google.com""http://www.google.com");
$ad[] = array("Title 8""Some description for Ad 8""www.google.com""http://www.google.com");
$ad[] = array("Title 9""Some description for Ad 9""www.google.com""http://www.google.com");

// Function to display random ads from the list
function show_ads($number_of_ads=1){
    // Loading the ads list
    global $ad;

    // maximul number of ads = number of ads
    if($number_of_ads count($ad)){
        $number_of_ads count($ad);
    }

    // Initialize the random generator
    list($usec$sec) = explode(' 'microtime());
    srand((float) $sec + ((float) $usec 100000));

    // select random ads
    $rand_keys array_rand($ad$number_of_ads);

    // creade ads
    $show_ads "<table><tr>";
    for($i=0$i<$number_of_ads$i++){
        $e $ad[$rand_keys[$i]];
        $show_ads .= "
<td onClick=\"document.location='{$e[3]}'\" title='{$e[0]}' width='180' style='cursor:pointer'>
<a href='{$e[3]}' style='font:13px Arial; font-weight:bold; color: #0000ff'>{$e[0]}</a><br>
<span style='font:12px Arial; color: #333333'>{$e[1]}</span><br>
<a href='{$e[3]}' style='font:10px Arial; color: #009900'><i>{$e[2]}</i></a>
</td>
";
    }
    $show_ads .= "</tr></table>";

    echo $show_ads;
}

?>

Tags:

One Response to “Simple Ad Rotator”

  1. Vood Says:

    Can you please tell me how i can display and rotate several 125×125 graphics on a page. Also can you show me how to format how they will appear on the page? The graphics that I need to rotate are here… http://www.phatfiber.com/thismonth1.htm
    Thanks in advance for any help. I don’t know much about how to use PHP script. Thank you.

Leave a Reply