How To HQ

How To Do Almost Anything

You are here: Home / Website Development / Simple PHP Banner Ad Rotator Script
Previous article: Mozilla Thunderbird Tips and FAQs
Next article: Thunderbird Tutorial – Using Spam and Junk Mail Filters

Simple PHP Banner Ad Rotator Script

August 3, 2007 by Doogie - Copyright - All Rights Reserved

Banner ad rotators can range in complexity from simple systems that randomly display banner or sidebar ads, to very complex systems that track every click on each ad and display different ads based upon preset percentages for each ad display.

This snippet of PHP code is the simple version. As many ads as you want can be loaded into an array and the code will randomly select an ad to display every time the page is loaded. This is a good solution for someone who wants to randomly display either advertising links or images on a page. It works well with banner image links found with many banner exchange programs and affiliate marketing programs. Simply substitute the code provided by your affiliate network or advertising partners for the value part of the name-value pairs in the following code.

Here is the code for the simple PHP banner ad rotator:

<?php
$bannerAd[1] = 'code for ad 1';
$bannerAd[2] = 'code for ad 2';
$bannerAd[3] = 'code for ad 3';
$bannerAd[4] = 'code for ad 4';
$bannerAd[5] = 'code for ad 5';

$adCount = count($bannerAd);
$randomAdNumber = mt_rand(1, $adCount);
echo $bannerAd[$randomAdNumber];
?>

Here’s how it works:

HTML or JavaScript code is loaded as a single line within a cell in the numeric array. $adCount determines the number of elements in the array. The array must start with an element numbered as 1, but can contain as many elements as you want. Don’t leave any gaps in the numbering sequence. A random number is generated from 1 to the $adCount value and stored in $randonAdNumber, which then displays the banner ad code for that designated cell in the array. All you need to do is to cut and paste the code into a Web page on a PHP enabled server and substitute your ad code in the array.

Either HTML or JavaScript code can be loaded into each cell of the array. Double quotes used with attribute values do not present a problem, but single quotes sometimes do cause problems. Note that single quotes are used to delimit the contents of a cell. If you run into PHP errors related to the array, look for single quote characters that may be causing a problem. You can either eliminate them or convert them to double quotes.

The following is an example of the hyperlink code for an advertising link. The code should be on a single line in your script.

$bannerAd[1] = '<a href="http://www.tech-evangelist.com/" 
target="_blank"><img src="/images/banner01.gif"
width="468" height="60" border="0"></a>';

If you want to use this script to randomly display images, just eliminate the hyperlink code and use the image portion of the code like the following.

$bannerAd[1] = '<img src="/images/banner01.gif"
width="468" height="60" border="0">';

Remember to place this on a single line in your script. 😀

  • Home

Categories

  • Automotive
  • Cooking
  • Energy
  • FileZilla Tutorials
  • Financial
  • Health
  • Home Electronics
  • Home Improvement
  • Internet
  • Kindle Tips
  • Miscellaneous
  • Outdoor Sports
  • Personal Computers
  • Pets
  • Security
  • Small Business
  • Thunderbird Tutorials
  • Website Development
  • WordPress
  • Yard & Garden
Content and images are copyrighted by HowToHQ.com and others
Content is intended for personal use only and may not be published or distributed on other websites

Copyright © 2006 - 2023 by HowToHQ.com - All Rights Reserved


Privacy Policy :: Terms of Use