Make Homepage | Add To Favorites | Print Page | Submit News | Feedback | Contact | 

Your Technical Computer Information Resource!  
     
  Weighted Banner Rotating System with PHP  
TACKtech Corp. > Articles > Software > Web Page Editing and Hosting

Weighted Banner Rotating System with PHP (TTID #187)

Author: Travis   Views: 22,307 /  Created: September 21, 2002
For the PHP people out there I converted the ColdFusion weighted rotating banner system. It is two files. Here is the first. I called it banner.php. You can call this file from a SSI include and it will run. The PHP and ColdFusion versions both do exactly the same thing in the same way just written for the different languages. Your database needs to be designed like so.

Field Name Type Description
ID Integer Auto incrementing
BANNER_ALT Text Alt description
BANNER_PIC Text Picture location
BANNER_URL Text URL link for banner
BANNER_WEIGHT Integer A weight from 0-10
BANNER_CLICKS Number How many times clicked
BANNER_VIEWS Number How many times viewed




<?php
$dbname = "<--database_name-->"
@ $db = mysql_connect("<--localhost-->", "<--username-->","<--password-->");
if (!$db)
{ echo "Error: Could not connect to DB.";
exit;}

// -- Start Banner Array --
$query = "select * from banners order by ID asc";
$results = mysql_db_query ($dbname,$query,$db);
//$prodlist = array ();
while ($row = mysql_fetch_array($results)){
for ($x=1; $x <= $row[banner_weight];$x++)
{$prodlist[] = $row[id];}
}
srand ((double) microtime() * 1000000);
$magicnumb = rand(0, count($prodlist)-1);
$magicnum = $prodlist[$magicnumb];
// -- End Banner Array --
// -- Start Banner View --
$query2 = "select * from banners where id = $magicnum";
$results2 = mysql_db_query ($dbname,$query2,$db);
while ($row2 = mysql_fetch_array($results2))
{
$add = $row2[banner_views] + 1;
$query3 = "update banners set banner_views = $add where id = $row2[id]";
mysql_db_query ($dbname,$query3,$db);
print "<br>";
// -- End Banner View --
// -- Start Display Banner --
print "<A HREF=\"banner/redirect.php?id=$magicnum\" TARGET=\"_blank\">";
print "<IMG SRC=\"$row2[banner_pic]\" alt=\"$row2[banner_alt]\" width=\"468\" height=\"60\"></A>";
// -- End Display Banner --
}
mysql_close ($db);
?>




The second file I called redirect.php. This page will add 1 to the click value for the banner in the database and then redirect to that site.



<?php
// -- Redirect Script --
$dbname = "<--database_name-->"
@ $db = mysql_connect("localhost", "<--username-->","<--password-->");
if (!$db)
{ echo "Error: Could not connect to DB.";
exit;}

// -- Start add one to Clicks --
$query = "SELECT * FROM banners where id = $id";
$results = mysql_db_query ($dbname,$query,$db);
while ($row = mysql_fetch_array($results)){
$add = $row[banner_clicks] + 1;
$query2 = "UPDATE banners set banner_clicks = $add where id = $row[id]";
mysql_db_query ($dbname,$query2,$db);
// -- End add one to Clicks --
// -- Start Redirect --
print "<META HTTP-EQUIV=\"Refresh\" Content=\"0; URL=http://$row[banner_url]\">";
}
// -- End Redirect --
mysql_close ($db);
?>





Your Name:


Your E-Mail: (required)


Friend's E-Mail: (required)




View Our World Wide Web Customer Privacy Policy
  Featured Articles  
  Quick Links  
  Top Affiliates  
........