So you need a simple banner ad rotator for your website. In this post I will guide you how to create one in PHP. No databases are needed for this. So first off open up your text editor or whatever program you use and create a new PHP document and call it something like banner468x60.php, this is so later you can include it into your templates so the banner shows up on your website!
The Settings
First we need to decide all the settings that we will need to include. We will need variables for the banner width and banner height. So to start of we will declare these.- <?php
- // settings
- $bannerWidth = 468;
- $bannerHeight = 60;
The Banners
Now we should create an array which will hold all the banner details. The details we will need are the url location, banner image url and a title. So below the settings we just declared we will need to create the array.- // banner array
- $bannerStore = array();
- // declare all the banners to be rotated, remember to enter your own banner details
- $bannerStore[] = array(
- url => "http://www.domain1.com",
- image => "http://www.domain1.com/img/banner01.png",
- title => "Cool Website 01"
- );
- $bannerStore[] = array(
- url => "http://www.domain2.com",
- image => "http://www.domain2.com/img/banner01.png",
- title => "Cool Website 02"
- );
Making It Rotate
Now we need to make it so the banners that you have setup can rotate and be displayed.- // count how many banners have been setup
- $count = count($bannerStore);
- // if there is 1 or more banners then output it
- if ($count) {
- // reduce the count by 1 since arrays begin at 0
- $count--;
- // get a random number from 0 - $count
- $rand = mt_rand(0, $count);
- // get banner details
- $url = $bannerStore[$rand]['url'];
- $image = $bannerStore[$rand]['image'];
- $title = $bannerStore[$rand]['title'];
- // output banner
- echo "<a href='{$url}' title='{$title}'>";
- echo "<img src='{$image}' alt='{$title}' width='{$bannerWidth}' height='{$bannerHeight}' border='0'>";
- echo "</a>";
- }
- ?>
0 komentar:
Posting Komentar