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:

6 Responses 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.

  2. val Says:

    I like this … I’m looking at it, wondering how I can replace the ‘text’ with Images – so I can display Banner ads, and/or Text ads – This would allow me to cut out middle men and advertisers can pay ME for spots on my site, or websites; It’s late; im going to bed; Im going to contact you about this soon; any ideas on Displaying the Banners would be great :)

  3. Christina Says:

    Yes, you can use images. When I was the webmaster for gotostcroix.com and during the redesign we needed to rotate the paid advertisers on the appropriate pages, e.g., resorts.

    I also used more fields //USAGE $ad[] = array(“Images”, “Ad Title and contact info”, “display url”, “URL”,”ad copy”);

    I modified the above to script to:

    // create ads
    $show_ads = “”;
    for($i=0; $i<$number_of_ads; $i++){
    $e = $ad[$rand_keys[$i]];
    $show_ads .= ”

    {$e[0]}

    {$e[1]}
    {$e[2]}

    {$e[4]}

    “;
    }
    $show_ads .= “”;

    echo $show_ads;
    }

    ?>

    Example of code:

    $ad[] = array(“”, “Main Title
    Subtitle
    Phone”, “domainname.com”, “http://www.domainname.com/”, “Text for ad goes here.”);

    You can see it in action here: http://www.gotostcroix.com/resorts.php

  4. Christina Says:

    Okay, not all of the code came through:

    // creade ads
    $show_ads = “<table width=’100%’ border=’0′ cellspacing=’0′ cellpadding=’5′ class=’ads’>”;
    for($i=0; $i<$number_of_ads; $i++){
    $e = $ad[$rand_keys[$i]];
    $show_ads .= ”

    <tr><td colspan=’2′ width=’100%’><br /><hr /><br /></td></tr>
    <tr><td width=’200′><div align=’center’>{$e[0]}</div></td>
    <td><p>
    {$e[1]}
    <a href=’{$e[3]}’ style=’font:10pt Arial; color: #009900′ target=’_blank’>{$e[2]}</a></p>

    <p style=’text-align:justify;’>{$e[4]}</p>
    </td></tr>
    “;
    }
    $show_ads .= “</table>”;

    echo $show_ads;
    }

    ?>

  5. tantan Says:

    Nice! I spent a couple of hours to find this script. Finally I got it now. Thank you very much!

  6. Christina Lannen Says:

    The client was just upgraded to php5 and now the script will not produce a random sort. I do not see anything obvious.

    Any ideas?

Leave a Reply