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 3

(Page 3 out of 3)

Creating the graphs

First head over to the homepage of PHP/SWF Charts and have a look at the demo's and tutorials to get a good feeling of how it looks and how it works.

The first graph we'll be creating is a simple one: the impressions and clicks plotted out against the months. For this we need two scripts: one that will display the chart (charts.php), and another to actually create the monthly chart (chart-month.php).

The code for the charts.php file is quite simple, and only contains some basic PHP:

include '../charts.php';

echo InsertChart ( "../charts.swf", "../charts_library", "chart-month.php?ad=1&" . time(), 500 );

What this code does is include the PHP/SWF Charts library, and then uses the insertChart function to insert the monthly chart. One thing you might notice is that I use the time() function to reference the chart. This is to prevent any browsers from caching the chart.

The chart-month.php file must first select all the data from the last 6 months, and then transform it into the proper data form that PHP/SWF Charts needs. If you have a look at the documentation you'll notice that it needs to be in a really specific form.

The code that does what we want looks like this:

include 'mysql.php';
include "../charts.php";

// Get ad id
if (!isset($_GET['ad'])) { no_chart(); }
$ad = intval($_GET['ad']);

// Get data for last six months
$months = $db->sql_query ("SELECT year, month, views, clicks, ctr FROM stats WHERE ad = $ad AND type = 'month' ORDER BY month DESC LIMIT 0,6");

if (count($months) == 0) {

$months = array_reverse($months);

// Create chart data
$monthnames = array('');
$clicks = array('Clicks');
$views = array('Impressions');

foreach ($months as $month) {
        $monthnames[] = date('F', mktime(0, 0, 0, $month['month'], 1, $month['year']));
        $clicks[] = $month['clicks'];
        $views[] = $month['views'];

$chart['chart_data'] = array($monthnames, $clicks, $views);

The above code first selects all the data of the last six months, and then transforms it into the right form, just like we want. Now add some formatting options:

$chart['axis_value'] = array('color' => 'FFFFFF');
$chart['axis_category'] = array('color' => 'FFFFFF', 'size' => '10');
$chart['axis_ticks'] = array('value_ticks' => true, 'minor_color' => 'FFFFFF');

$chart['chart_type'] = 'stacked column';

$chart [ 'chart_value' ] = array ('position' =>  "middle", 'bold' =>  true, 'size' =>  11, 'color' => "FFFFFF", 'alpha' => 190);

And finalize the chart with the SendChartData() function that comes with the PHP/SWF library:

SendChartData ($chart);

And the end result should look something like this:

Chart 1

If you want to know what each formatting option means, have a look at the PHP/SWF Reference for a complete explanation on each option.

Now we have a neat graph showing the impressions and clicks of the last 6 months. The same thing can be done for the last 31 days, or the last week. All the above code stays pretty much the same, except for a few minor adjustments. It's rather pointless to copy all the code from chart-week.php and chart-day.php here, so click here to download chart-week.php and here to download chart-day.php.

If you combine all three chart files, and use another page to show all the charts, the end result looks something like this: charts.php (source of charts.php). Looks pretty professional doesn't it?


In this part of the series I have shown you how to gather statistics of each ad, and how to create great-looking charts with the PHP/SWF Charts library. The charts we created are really simple, and if you browse through the Chart Gallery you will come across much better looking charts.

This was the final part of the 'Building an advertising system, with PHP' series, and I hope I've shown you how you can create your own advertising system for your website or network. If you combine all the examples and code I've given you, together with some custom changes, you can have a very professional ad system for your website and its advertisers.

In the future I might release an open-source advertising system, based on this series, but I can't promise anything. I do have to admit though that I can't wait to use PHP/SWF Charts in one of my projects, as it's really a great library.

If you have any comments or suggestions, leave them in the comments or post them on the forums.

Click here to download the demo files of this part (Note that you need to download PHP/SWF Charts yourself)

« Previous: Analyzing the traffic

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 & Gathering Statistics
  2. Analyzing the traffic
  3. Creating the graphs & Conclusion
Bookmark Article
Download Article
Download this article as a PDF file