Dismounted
59.***.***.***
Simple Ad Rotation
Simple Ad Rotation
Create a new file (ads.php) and copy and paste the below code into that file, then save it.
<?php
$adfile = "ads.txt";
$ads = array();
$fh = fopen($adfile, "r");
while(!feof($fh)) {
$line = fgets($fh, 10240);
$line = trim($line);
if($line != "") {
$ads[] = $line;
}
}
$num = count($ads);
$idx = rand(0, $num-1);
echo $ads[$idx];
?>
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 name in 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>
This actually will randomly display an ad, their is no configuration to always display ads in order. It works well and saves space. Now, back to the code. This script uses basic php codes to gather information, and a simple randomizer.
Great contribution, many people new to PHP begin their journey with simple randomizer scripts.
One note --- you can save a little on the performance side of things by storing the ads in an internal array() to begin with instead of an external file that must first be read.
<?
$ads=array("<a href=\"http://www.advertiser1.com\"><img src=\"img1.gif\"></a>","<a href=\"http://www.advertiser2.com\"><img src=\"img2.gif\"></a>","etc, as many or as few as you would like");
echo $ads[rand(0,count($ads)-1)];
?>
Dismounted
59.***.***.***
Yes, but my way looks much "cleaner" and easier to understand for those newcomers (otherwise referred to as 'noobs' :)). With your code, spacing the array out would look better :p.
Darkneoboi
68.***.***.***
I found this to be a great help since at another forum i work at we are making a banner ad rotator. I think I can modify that script enough to work with a database since that is what the script will be powered by.
Dismounted
59.***.***.***
If you need any help with that, post in the forum.
Darkneoboi
68.***.***.***
Don't worry, I will. Also how would I do a mysql_num_rows and then -1 that and do 0 to num_of_rows-1 as an array and have it choose one of those?
penguinmama
12.***.***.***
Just guessing here... but my thoughts are:
$qty = mysql_num_rows($result);
$qty = $qty-1;
Well, there my knowledge breaks down and I don't know how to get the rest into an array... unless you can use array_push (not sure of the exact syntax, but something where you foreach step through all the numbers from 0 to $qty and push 'em into an array. I'd have to look up *how* but that's what I'm thinking.
In my opinion it would be better to just use the in-built MySQL randomizer feature.
SELECT * FROM tablename ORDER BY rand() LIMIT 1
penguinmama
12.***.***.***
That's a nice bit to know! Yes, that does seem like it would be easier.
Though I still think my way would have worked ;-)