Log in

View Full Version : Create a random banner advertisement rotator



Rockstar
07-11-2007, 04:36 PM
<?php // Start PHP so the server knows to parse anything below this as PHP
$ads = array(); // Start an array

$ads[] = '<a href="http://www.domain.com"><img src="/banners/ad1.gif" alt="domain's banner" /></a>';
// We have created our first ad. We simple just put the format of the ad within an $ads[] variable. We can add as many ad's as we want as it is an array and won't overwrite the variable.

$ads[] = '<a href="http://www.domain2.com"><img src="/banners/ad2.gif" alt="domain2's banner" /></a>';
//We have no added another. Now we will display the ad's.

shuffle($ads);
// We will use the PHP function shuffle to randomise our advertisements.

echo $ads[0];
// We echo the first ad in the array. The array has been shuffled so the first ad will change.

?> //Stop parsing as PHP

Im newb to PHP so yeh
you have now created your ad's

MrCraig
07-11-2007, 04:39 PM
Cool tut =]

Maybe use the
PHP tags though while posting?

Rockstar
07-11-2007, 04:41 PM
Cool tut =]

Maybe use the
PHP tags though while posting?
I didnt know how to I tryed
But didnt work

Blob
07-11-2007, 04:59 PM
Sorry, I just had to edit it.



<?php // Start PHP so the server knows to parse anything below this as PHP
$ads = array(
'<a href="http://www.domain.com"><img src="/banners/ad1.gif" alt="domain\'s banner" /></a>', '<a href="http://www.domain2.com"><img src="/banners/ad2.gif" alt="domain2\'s banner" /></a>'
);
//We have no added another. Now we will display the ad's.

$rand = rand(0, count($ads);
// We will use the PHP function shuffle to randomise our advertisements.

echo $ads[$rand];
// We echo the first ad in the array. The array has been shuffled so the first ad will change.

?> //Stop parsing as PHP
Should work

Want to hide these adverts? Register an account for free!