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:
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:
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.
468x60 banner
250x250 cube
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:
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:
$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');
}
}
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.
December 18th, 2005 at 9:24 pm
[…] 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. […]
July 19th, 2006 at 8:34 am
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.