ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

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

Advertisements

Building an advertising system with PHP, Part 2

(Page 2 out of 3)

Feature #1: Different Types

At the moment our ad system can only serve one type of ads, but what if you've got two different spots on your website, e.g. a 468x60 banner and a 250x250 cube? Our ad system needs to be extended, and be able to serve different kinds of ads.

The first thing we must do is add a new field to our ad table, which looks like this:

ALTER TABLE `ad` ADD `adtype` VARCHAR( 255 ) NOT NULL ;

Now we have to add this feature in our ad system, which thankfully is quite easy. All we have to do is add a new query string (type), and pass it to the database, like so:

// Get type
if (isset($_GET['type'])) { $type = ss($_GET['type']); } else { $type = ''; }

// 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 AND adtype = '$type' ORDER BY rand() LIMIT 0,1");

If you copy the code above, and put it in your ad system (replacing the original query), then it's possible to select ads by type. If you connect this with the right ad code, it works perfectly, e.g.


       
                </span>Ad System (Part 2)<span class="sc2"><span class="kw2">
       

       

       

468x60 banner


        src="ad2.php?type=468x60a" type="text/javascript">

       

250x250 cube


        src="ad2.php?type=250x250a" type="text/javascript">

       

(View Live Demo)

Another added benefit is that's possible to create any type we like. In the above example I created two types called '468x60a' and '250x250a', but you can easily call your types something completely different, like 'topbanner', 'contentad', etc. This allows you to create different zones of advertising on your website.

Let's have a look at creating priority ads now.

Feature #2: Priority Ads

Like the previous feature, we first have to add a new field to our ad table. This time the field looks like this:

ALTER TABLE `ad` ADD `priority` ENUM( 'yes', 'no' ) DEFAULT 'no' NOT NULL ;

As you can see an ad can be either a priority ad (yes) or not (no). All our advertising system has to do is first select the priority ads, and display a priority ad, or get a regular ad if there aren't any priority ads. Sounds easy, and that's because it is easy!

The code below does what we want:

// Priority Ads:
$ad = $db->query_first ("SELECT adid, link, image, impressions, viewed FROM ad WHERE impressions-viewed > 0 AND adtype = '$type' AND priority = 'yes' ORDER BY rand() LIMIT 0,1");

// Are there any priority ads? If not, get regular ads
if ($ad == false) {
        // Regular Ads:
        $ad = $db->query_first ("SELECT adid, link, image, impressions, viewed FROM ad WHERE impressions-viewed > 0 AND adtype = '$type' AND priority = 'no' ORDER BY rand() LIMIT 0,1");

        // Are there any regular ads?
        if ($ad == false) {
                // No ads:
                die('// No ad\'s');
        }
}

(View in action)

Priority ads will now also be displayed before regular ads, and it'll no longer be (completely) random. Priority ads may seem like a stupid idea, but it can come in quite useful, especially when dealing with advertisers, who might want certain guarantees.

« Previous: Introduction & Database Move
Next: Management Page & Conclusion »



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
PDF
Download this article as a PDF file