website promotion banner
eturnkeys
Your Ad Here
Web Programming  Home Web Programming PHP Simple Ad Rotation
rss

Simple Ad Rotation

Author: ViButX More by this author


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="advertisers_image.gif" border="0"></a>
<a href="http://advertisement2_url.com" target="_blank"><img src="advertisers2_image.gif" border="0"></a>
<a href="http://advertisement3_url.com" target="_blank"><img src="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: www.dannyscripts.com

Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "Simple Ad Rotation"