PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP Simple Ad Rotation
Your Ad Here

Simple Ad Rotation


Simple Ad Rotation1. Create a new file (newfile.php) and copy and paste the below code into that file, then save it.

<?php

// load the file that contain the ads
$adfile  =  "ads.txt" ;
$ads  = array();

// one line per ad
$fh  =  fopen ( $adfile ,  "r" );
while(! feof ( $fh )) {

$line  =  fgets ( $fh ,  10240 );
$line  =  trim ( $line );
if( $line  !=  "" ) {
$ads [] =  $line ;
}
}

// randomly pick an ad
$num  =  count ( $ads );
$idx  =  rand ( 0 ,  $num - 1 );

echo  $ads [ $idx ];
?>

2. You are going to need to create a blank text file, (ads.txt). If you change the name of the file, make sure you change the bolded area of the code as well. Now to add your advertisements to the text file, just code an image with a hyper link and make sure that each ad is on its own line. Just look below for an example.

<a href="http://advertisement_url.com" target="_blank"><img src="/img_articles/11100/advertisers_image.gif" border="0"></a>
<a href="http://advertisement2_url.com" target="_blank"><img src="/img_articles/11100/advertisers2_image.gif" border="0"></a>
<a href="http://advertisement3_url.com" target="_blank"><img src="/img_articles/11100/advertisers3_image.gif" border="0"></a>

3. This actually will randomly display an ad, their is no configuration to always display ad's in order. This is the same rotation script that i use. It works well and saves space. Now, back to the code. This script uses basic php codes to gather information, and a simple randomizer.



Author's URL: ViButX
Thank you for voting.
Rate this Materials:
Bad 
1 2 3 4 5 Excellent
print this page subscribe to newsletter subscribe to rss

Web programming � everything from the basics of visual design and architecture to the specifics of applications, graphics, and scripting. More Web Programming: Most Popular Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Add comments to "Simple Ad Rotation"

Only registered users can write comment

Reader's comments