ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.


Building an advertising system with PHP, Part 2

(Page 1 out of 3)


Welcome to part 2 of the "Building an advertising system with PHP" series. In the previous part I have shown you how to set up our basic advertising system, and how the JavaScript works. If you haven't read that part yet, I highly suggest you do that before reading this part.

In this part we'll be making our advertising system slightly more advanced, and move from using files to a MySQL database. We'll also add a management page for our ads, and two new features (priority ads and different types of ads). Let's start with moving to a database.

Moving to a database

Before writing any code, let's first create the database for our advertising system. At the moment it needs only one table, which looks like this:

  `adid` mediumint(10) NOT NULL AUTO_INCREMENT,
  `link` varchar(255) NOT NULL DEFAULT '',
  `image` varchar(255) NOT NULL DEFAULT '',
  `impressions` int(5) NOT NULL DEFAULT '0',
  `viewed` int(5) NOT NULL DEFAULT '0',
  PRIMARY KEY  (`adid`)

The impressions field is used to limit the amount of impressions an ad can get, and the viewed field is used to keep track how many views an ad has received. Now that you've got the database and table set up, enter a few ads so you can test the system. Make sure to give a decent amount of impressions (i.e. 1000) or else the ads won't be shown after some testing.

Now we must convert our ad system from using the log file (from part 1) to using our database. This is extremely easy, and the code for our ad system is actually a lot shorter now. The ad.php file now looks like this:

include 'mysql.php';

// Get a random ad from DB (and only get an ad that has impressions left)
$ad = $db->query_first ("SELECT adid, link, image, impressions, viewed FROM ad WHERE impressions-viewed > 0 ORDER BY rand() LIMIT 0,1");

if ($ad == false) {
        // No ads:
        die('// No ad\'s');
$link = $ad['link'];
$image = $ad['image'];

// Display ad:
header ("Content-Type: text/javascript");
echo "document.write ('\"$link\">\"$image\" alt=\"Ad Banner\" style=\"border:0;\" width=\"468\" height=\"60\" />');";

// Update viewed count
$db->query ("UPDATE ad SET viewed = viewed + 1 WHERE adid = " . $ad['adid']);

What the above code does is first pulls a random ad, that still has impressions left, from the database, and then displays it. At the end it also updates the viewed count. This code is a lot cleaner and shorter than our previous code, which just goes to show that using a database is usually a lot easier than using flat files.

Note that I included a separate database file that can be downloaded here: mysql.php. It's just a basic MySQL database class, and some connection variables.

Now that the system has moved to a database, let's add two new features: priority ads and different types.

Next: Different Types & Priority Ads »

2 Responses to “Building an advertising system with PHP, Part 2”

  1. PHPit - Totally PHP » Building an advertising system with PHP, Part 3 Says:

    […] Welcome to part 3 of the “Building an advertising system with PHP” series. In the previous parts (part 1 and part 2) I have shown you how to build your own advertising system using PHP and JavaScript. We’ve also added two extra features to our ad system and in part 2 we built a page to manage the ads as well. If you haven’t read either part yet, I highly recommend doing so before reading this part. […]

  2. Anup kumar singh Says:

    Thanks for this wonderful information, I am having only 6months of exp in PHP, I was assigned the advertisement task, from last 2days i was wondering how to create the advertisement module and i found this information this is very much informative and i will try to add more features to this system and will forward it to you.

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Database Move
  2. Different Types & Priority Ads
  3. Management Page & Conclusion
Bookmark Article
Download Article
Download this article as a PDF file