PHP Banner Rotator Tutorial
Ranked #1,626 in Internet, #95,035 overall
Creating a php banner rotation script
Preview: Click here.
Creating the database
Our script will need to save 7 things for each banner: the image url, link url (page to go to upon click on the banner), the location to display the banner on, views and max amount of views the banner may receive. We'll create a database table to store these info into: we'll call the fields: img_url, link_url, location, views, max_views and we'll have one more field called id.
SQL
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
CREATE TABLE IF NOT EXISTS `banners` (
`id` int(250) NOT NULL AUTO_INCREMENT,
`img_url` varchar(250) NOT NULL,
`link_url` varchar(250) NOT NULL,
`location` varchar(250) NOT NULL,
`views` int(11) NOT NULL,
`max_views` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
INSERT INTO `banners` (`id`, `img_url`, `link_url`, `location`, `views`, `max_views`) VALUES
(1, 'http://img856.imageshack.us/img856/4108/bannerct.png', 'http://www.squidoo.com/lensmaster/webcodez', '*', 4, 1000),
(2, 'http://www.istad.org/lenses/buttons-banners/squidoo-banner-teal.gif', 'http://www.squidoo.com', '*', 4, 1000);
Creating the banner rotation script
config.php
<?php
/*
DB CONFIG
*/
$db['server'] = "localhost";
$db['user'] = "root";
$db['pass'] = "";
$db['name'] = "webcodez";
/*
DB CONNECTION
*/
mysql_connect($db['server'], $db['user'], $db['pass']);
mysql_select_db($db['name']);
?>
Next we'll start creating the actual rotator script. We'll simply create one function that gets the banner to display. We'll call it "displayBanner":
rotator.php
function displayBanner($location) {
}
... WHERE location = '$location' OR location = '*'
thus we get the following query:
SELECT * FROM banners WHERE location = '$location' OR location = '*'
But this would select ALL the banners that may be displayed on the current page. We only want 1 though, a random one. To do this, we can use the following 'trick': adding a random order to the query using ORDER BY RAND():
<?php
function displayBanner($location) {
$get_banner = mysql_query("SELECT * FROM banners WHERE location = '$location' OR location = '*' ORDER BY RAND()");
}
?>
<?php
function displayBanner($location) {
$get_banner = mysql_query("SELECT * FROM banners WHERE location = '$location' OR location = '*' ORDER BY RAND()");
$banner = mysql_fetch_assoc($get_banner);
}
?>
<?php
function displayBanner($location) {
$get_banner = mysql_query("SELECT * FROM banners WHERE location = '$location' OR location = '*' ORDER BY RAND()");
$banner = mysql_fetch_assoc($get_banner);
echo "<a href='".$banner['link_url']."'><img src='".$banner['img_url']."'></a>";
}
?>
rotator.php
<?php
function displayBanner($location) {
$get_banner = mysql_query("SELECT * FROM banners WHERE location = '$location' OR location = '*' ORDER BY RAND()");
$banner = mysql_fetch_assoc($get_banner);
echo "<a href='".$banner['link_url']."'><img src='".$banner['img_url']."'></a>";
$update_banner = mysql_query("UPDATE banners SET views = views+1 WHERE id = '".$banner['id']."' ");
}
?>
Usage
<?php
/*
My Webpage!
*/
include_once("config.php");
include_once("rotator.php");
displayBanner($_SERVER['PHP_SELF']);
?>
To add a banner you can simply create a query like this:
mysql_query("INSERT INTO banners(img_url, link_url, location, max_views)VALUES('image url here', 'link url here', 'location', max views here) ");
This can also be done through phpmyadmin or whatever software you/your webhost uses for database management. In this tutorial we used $_SERVER['PHP_SELF'] as location reference for each file, so you'll have to use that format for the location of each file.You might also like ...
Comments & Questions
-
-
rocky
Mar 24, 2012 @ 2:16 am | delete
- thanks for this i will try
mobile prices in Pakistan
-
by webcodez
"Give a man a program and you'll frustrate him for one day. Teach a man to program and you'll frustrate him for a whole lifetime."
webcodez
GameHeroes
more »
- 21 featured lenses
- Winner of 14 trophies!
- Top lens » PHP & MySQL Poll System Tutorial
Explore related pages
- PHP basics tutorial PHP basics tutorial
- MySQL Basics Tutorial MySQL Basics Tutorial
- PHP & MySQL Poll System Tutorial PHP & MySQL Poll System Tutorial
- PHP & MySQL Visitors Counter Tutorial PHP & MySQL Visitors Counter Tutorial
- PHP & MySQL Login System Tutorial PHP & MySQL Login System Tutorial
- How to install PHP & MySQL scripts How to install PHP & MySQL scripts