Building an advertising system with PHP, Part 2
(Page 3 out of 3)Creating an admin panel
Let's have a look at creating a simple admin panel so the ads can be managed online, without having to use phpMyAdmin.
What our admin panel must be able to do are the following things:
- Add new advertisements
- Edit existing advertisements
- Delete existing advertisements
The easiest way of doing this is to include all three features in the same file, and that's exactly what we're going to do.
The code inserting a new advertisement looks like this:
$link = ss($_POST['link']);
$image = ss($_POST['image']);
$adtype = ss($_POST['adtype']);
$priority = ss($_POST['priority']);
// Check everything, and insert new ad
if (!empty($link) AND !empty($image) AND !empty($adtype) AND !empty($priority)) {
$db->query ("INSERT INTO ad (link, image, adtype, priority) VALUES ('$link', '$image', '$adtype', '$priority');");
}
And the code for updating an advertisement is quite similar:
$id = intval($_GET['id']);
$link = ss($_POST['link']);
$image = ss($_POST['image']);
$adtype = ss($_POST['adtype']);
$priority = ss($_POST['priority']);
// Check everything, and update ad
if (!empty($link) AND !empty($image) AND !empty($adtype) AND !empty($priority)) {
$db->query ("UPDATE ad SET link = '$link', image = '$image', adtype = '$adtype', priority = '$priority' WHERE adid = $id");
}
And the code deleting an advertisement looks like this:
$id = intval($_GET['id']);
$db->query ("DELETE FROM ad WHERE adid = $id");
Combine the above three code snippets, and what you get is the following: manage.php
You're probably wondering how the above code gives that result, but the above code is actually the most important and the other things I've added are merely sugar coating, like some HTML and design. Don't worry though, as you can download the full source code at the end of this article.
Conclusion
In this article I have shown you how to move our advertising system to a MySQL database, and we've added two new features. In addition, we've also created an ad management page. There are countless more things we can add, but I'll leave that as an exercise. Think about adding targeting features (e.g. detecting the user's language, and serving the right ad) or a new page where your advertisers can edit their own ads.
In the next and final part of this series we will extend our advertising system to collect statistics on each ad, like how many times has it been served, who has clicked, what the CTR (click-through-rate) is, etc. We'll be using PHP/SWF Charts to create really neat flash graphs.
If you have any comments on this article, head over to PHPit Forums or leave any comments below.
Click here to download the demo's and source code.
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.