
Home | Invoicing | Resume | Feedback | Guestbook | Personal Pages | Other Web Sites | Anything from Amazon!
MySQL to XML Export with PHP Random PHP Banner Ads
This is a simple way to add banner ads to your pages. It is how we're generating the banner ads on this site (names and passwords changed to protect the
site!) If you're running PHP with a MySQL backend, simply create a table as below. Add security to the table as required for your site. You can
set the name and password to be used with the $dbUserID and $dbUserPassword variables. $ads_displayed lets you pump out multiple ads if necessary. The
code is generated to let you use one database for multiple websites. The banners chosen are based on the website calling the code when it displays.
I've included a sample page used for editing the ads below called edit.php. This simply will show all the ads you currently have, allowing you to add and
delete code for the ads.
The format for the ads that you will enter is something like the following:
- <a href="http://www.adserver.com/click-1187206-8154271" target="_blank"><img src="http://www.adserver.com/image-1187206-8154271" alt="Big Savings! FREE
Shipping!" border="0"></a>
Many of the commercial ad service companies will give you the links in this format, so this is how we accept it. Just copy and paste from their link setup
page right into the edit.php page.
SQL Definition
- DROP DATABASE ads;
CREATE DATABASE ads;
CONNECT ads;
- CREATE TABLE ads (
- id int(11) unsigned NOT NULL auto_increment,
- site varchar(64) default NULL,
- url text,
- PRIMARY KEY (id)
- ) TYPE=MyISAM;
banner.php
- <p align="center">
- <?php
- $dbUserId="";
- $dbUserPassword="";
- $dbServer="localhost";
- $dbName="ads";
- $ads_displayed = 1;
- $conn = mysql_pconnect($dbServer,$dbUserID, $dbUserPassword) or die("Unable to connect to SQL server");
- $db = mysql_select_db($dbName, $conn) or die("Unable to select database");
- $query = "SELECT * FROM ads WHERE site = '$SERVER_NAME' ORDER BY RAND() LIMIT $ads_displayed";
- $result = mysql_query($query, $conn);
- while ($row = mysql_fetch_assoc($result)) {
- echo ($row["url"] . "<br>");}
- ?>
- </p>
edit.php
- <?php
$dbUserId="";
$dbUserPassword="";
$dbServer="localhost";
$dbName="ads";
$ads_displayed = 1;
$conn = mysql_pconnect($dbServer,$dbUserID, $dbUserPassword) or die("Unable to connect to SQL server");
$db = mysql_select_db($dbName, $conn) or die("Unable to select database");
if ($_GET["ad"]) {
$query = 'SELECT * FROM ads WHERE id = ' . $_GET["ad"];
$result = mysql_query($query, $conn) or die(mysql_error());
$ad = mysql_fetch_object($result);
}
if ($_GET["action"] == "DELETE") {
$query='DELETE FROM ads WHERE id = ' . $_GET["ad"];
$result = mysql_query($query) or die(mysql_error());
}
if (($REQUEST_METHOD=='POST')) {
$query="REPLACE INTO ads " ;
$query .="(id, site, size, url)" ;
$query .=" values($ad_id, '$site','$size','$url')" ;
$result = mysql_query($query) or die(mysql_error()); }
?>
<html><head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<style>
<!--
.headline { font-style:bold; font-variant:normal; font-size: 14pt; font-family:verdana;}
.intro { font-style:normal; font-variant:normal; font-size: 10pt; font-family:verdana;}
.story { font-style:normal; font-variant:normal; font-size: 8pt; font-family:verdana;}
.storytd { border-bottom : thin solid #E6E6FA; }
.tdlink { font-style:normal; font-variant:normal; font-size:8pt; font-family:verdana; color:blue;}
a {color: #3366CC; background-color : transparent; text-decoration: none; font-weight: bold; }
a:hover { color: #FF00FF; text-decoration: underline; }
input, select, textarea {width: 99%;}
-->
</style></head>
<body>
<form method="POST" action="<? echo $PHP_SELF ?>">
<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
<tr>
<td width="18%">For Website</td>
<td width="82%"><input type="text" name="site" size="20" value="<?php echo ($SERVER_NAME); ?>"></td>
</tr>
<tr>
<td width="18%">Code</td>
<td width="82%"><input type="text" name="url" size="20" value="<?php echo htmlspecialchars($ad->url); ?>"></td>
</tr>
<tr>
<td width="18%"> </td>
<td width="82%"><input type="Submit" value="Submit" name="B1" style="width:auto">
<input type="button" value="DELETE" name="B2" style="width:auto" onclick="window.location.href='editAds.htm?ad=<? echo $ad->id; ?>&action=DELETE'"></td>
</tr>
</table>
<input type="hidden" name="ad_id" value="<?php echo ($ad->id?$ad->id:0) ?>">
</form>
<table><tr><td>
<?php
$query = "SELECT * FROM ads WHERE size = 'banner' AND site = '$SERVER_NAME'";
$result = mysql_query($query, $conn);
while ($row = mysql_fetch_assoc($result)) {
- echo ('<a href="editAdsShow.htm?ad='. $row["id"] . '">' . $row["id"] ."</a>". $row["url"] . "<br>");} ?>
- </td></tr></table></body></html>
On your webpages, simply include the following line wherever you would like a banner ad to appear. (Note that this has to be in the same
directory as your page. If you organize into subfolders, just include the banner code page in each subfolder.:
- <?php include("banner.php"); ?>