Hi, Guest ~ Login or Register

Random Forum Advertisements

Random Forum Advertisements

Listed In PHP and MySQL » Systems and Features — Viewing Full Tutorial

Introduction


Forums are a very hard aspect of a website to monetize. Often, it’s where most of the action takes place and it’s another powerful tool in generating organic traffic through the vast amounts of user generated information that is churned out on a daily, weekly and monthly basis.
Quite a few sites out there think its fine to place annoying double underlined link scripts all over the forums. I guess that’s fine – whatever rocks your world, even though the majority of users prefer otherwise.
The technique I’m about to go through is better than the one I’ve just mentioned, and this is why:
• This method is much more unobtrusive and the user is definitely going to see the ads.
• You don’t need JavaScript.
• Your users will be happier.
• You’ll probably make more money!


First things first


You’ll need to take into account all factors that you’re going to have to cater for to still be in line with your advertising scheme. In this instance, I’ll be optimizing the code and methods for Google AdSense. Google has a stringent set of webmaster guidelines, the most important of which I shall summarise below.
• No more than 3 Google AdSense advert modules can be present on the page at a time.
• No legally or ethically questionable content.
• No visual devices such as arrows can be used to draw attention to the ads.
The last two are down to you as the webmaster to stick to, but there’s an easy way to limit the amount of ads generated on a page which I’m going to include in this tutorial, which means you don’t have to worry.


Setting up the script


First of all we’ll need to declare a few variables to keep stuff random, and keep the density of the adverts in a reasonable post:advert ratio. For example, you could stop the ads displaying if there were less than 10 replies.
Let’s say we have a limit per page of 20 posts. You may well have defined this in your pagination functions, which is fine – you can use that variable instead. Assuming you’ve not got anything, we can set this to 20.
<?php
$limit = 20;
Now we need to find out the maximum number of placements we can have by using a ratio. For this example I’ve decided a 10:1 ratio looks fair enough.
$max_placements = ceil($limit/10); // Ceil is used here, but you can use floor or round instead :)

But we’re not done yet. What if you decide to display 40 posts? 40 divided by 10 is 4, which is over the limit Google has put on us, so we add these lines below using shortened if/else syntax to make sure we’re not breaking any rules.
$ad_limit = 3;
$max_placements = ($max_placements > $ad_limit) ? $ad_limit : $max_placements;

The last line above us tells PHP to reset $max_placements to $ad_limit if we’re over it, otherwise PHP will leave the numbers alone.
We’ve got our constraints sorted – just about. We now have to decide how many posts are required for adverts to be displayed. In this case I’ll set it to 5.
$ad_posts_required = 5;
Painfully simple. Now we’re going to set up an array of random placements to show the adverts using the variable we have just set, and the for loop. Please note, you’ll need to count how many replies there are to the thread which is totally down to you.
$num_posts = 17; // THIS IS AN EXAMPLE. YOU’LL NEED TO CALCULATE THIS YOURSELF.
$ad_show = array(); // This will be used to hold the random placements.
if($num_posts >= $ad_posts_required) {
  for($i = 1; $i <= $max_placements; $i++) {
    $ad_show[] = rand(1, $limit); // Add an index to $ad_show with a number between 1 and $limit
  }
}

With all of this code done, you should have the following (or something similar to it) inside your forums view code.
<?php
$limit = 20;
$max_placements = ceil($limit/10); // Ceil is used here, but you can use floor or round instead :)
$ad_limit = 3;
$max_placements = ($max_placements > $ad_limit) ? $ad_limit : $max_placements;
$ad_posts_required = 5;
$num_posts = 17; // THIS IS AN EXAMPLE. YOU’LL NEED TO CALCULATE THIS YOURSELF.
$ad_show = array(); // This will be used to hold the random placements.
if($num_posts >= $ad_posts_required) {
  for($i = 1; $i <= $max_placements; $i++) {
    $ad_show[] = rand(1, $limit); // Add an index to $ad_show with a number between 1 and $limit
  }
}
?>



Through the loop


Before we get to your output loop which will show the replies, there are a few things we need to take care of if you haven’t already yet.
We need to give each iteration of the loop an ID number. In this instance we’ll assign a variable outside of it called $reply_number and set it to 1. At the end of the iteration, we’ll increment it by 1. This is what we use to compare against our $ad_show array indexes:
$reply_number = 1;
// While loop code. This is an example!
while($reply = mysql_fetch_assoc($replies)) {
  // default output code here
  $reply_number++;
}

Obviously the comment signifying your output code is down to you, so essentially all you need to change is the first and penultimate lines to get that sorted.


Outputting your adverts


Having just read two or so pages just to get to this stage and to realise how simple it really is after all of that, you may be feeling a little miffed. Nonetheless, this is all you’ve gotta do:
So, you’ve got your $reply_number, right? We use this to search inside the $ad_show array using the in_array() PHP function. The arguments are as follows: in_array(MIXED NEEDLE, ARRAY HAYSTACK);
With this knowledge we can incorporate it into our forums code right after a post has been shown. We’re inside the output loop now, just after (assuming your forum uses table outputs) the </tr> tag for the reply template.
if(in_array($reply_number, $ad_show)) {
  <tr><td>Sponsored Link</td></tr>
  <tr><td colspan=”2” align=”center”>
  // your google adsense code here
  </td></tr>
}

This will be just before you increment your reply number. If you’re feeling a little lost, here’s what it’s meant to look like (roughly, of course!)
<?php
$reply_number = 1;
// While loop code. This is an example!
while($reply = mysql_fetch_assoc($replies)) {
  // default output code here
if(in_array($reply_number, $ad_show)) {
  <tr><td>Sponsored Link</td></tr>
  <tr><td colspan=”2” align=”center”>
  // your google adsense code here
  </td></tr>
}
  $reply_number++;
}
?>



Closure


That’s it! You’re literally finished and ready to unleash a can of contextually served whoopass on your forum users. I hope this implementation shines on your forum, and you benefit from it with every extra penny in your AdSense paycheques!

Working Beta

  1. The Forums
    These are mostly functional. If you see any weird bugs, post a thread about it and an administrator will do something.
  2. Tutorial Writing
    You can now submit tutorials to the brand new management system.
  3. Tutorials Home
    View tutorials by categories and search for them here.
  4. Shoutbox
    See below. Registered users only!

Register

Newest User

Say hi to hendy! hendy joined on Thursday, 15th May.

Sponsor

Check out Next day fake id

Shoutbox