First, create a new table on mysql:
CREATE TABLE `banners` (
`id` BIGINT( 255 ) NOT NULL AUTO_INCREMENT ,
`imageurl` VARCHAR( 255 ) NOT NULL ,
`alt` VARCHAR( 255 ) NOT NULL ,
`url` VARCHAR( 255 ) NOT NULL ,
`clicks` BIGINT( 255 ) NOT NULL ,
PRIMARY KEY ( `id` )
);
well, now lets insert a new banner on this table:
INSERT INTO `banners` ( `id` , `imageurl` , `alt` , `url` , `clicks` )
VALUES (
'', 'http://www.yoursite.com/images/banner.gif', 'Trap17 - The best free web hosting on the web', 'http://www.trap17.com', ''
);
Now, the page which will show the banners:
<?
mysql_connect("localhost","user","pass");
mysql_select_db("dbname");
$randbanner = mysql_query("SELECT * FROM banners ORDER BY RAND() LIMIT 1");
$i = 1;
while($banner = mysql_fetch_array($randbanner)){
$id = $banner["id"];
$imageurl = $banner["imageurl"];
$alt = $banner["alt"];
$url = $banner["url"];
$clicks = $banner["clicks"];
$banners[$i][img] = "$imageurl";
$banners[$i][url] = "click.php?id=$id";
$banners[$i][alt] = "$alt";
$r = rand($i,sizeof($banners)); // rotate
echo '<a href="'.$banners[$r][url].'" target="_blank"><img src="'.$banners[$r][img].'" alt="'.$banners[$r][alt].'" border="0"></a>'; // Show the banner
}
?>
Now, lets make clicks.php:
<?
mysql_connect("localhost","user","pass");
mysql_select_db("dbname");
$id = addslashes($_GET['id']); // addslashes is for security of your database
$clicks = mysql_query("UPDATE banners SET clicks=clicks+1 WHERE id='$id'");
$table = mysql_query("SELECT * FROM banners WHERE id='$id'");
$details=mysql_fetch_array($table);
$url = $details["url"];
$id = $details["id"];
echo '<meta http-equiv="refresh" content="1;url='.$url.'">'; // After 1 second, redirect for the URL of the banner
?>
That's all, if you have any questions just post here!

