tags:

views:

56

answers:

1

For each ad on my site, I would like a box that shows the number of views it has (similar to this site).

How would I capture the number of page views an ad has? Is there a function that does this in PHP?

+1  A: 

Well, no there is no function that does this magically : you'll have to do a bit of work -- not that hard, though ;-)

There are two possible things you can count.


For the first one, number of times an ad is displayed, the basic idea is :

  • Your are displaying an add -- you already know how to do that
  • When displaying it, you'll update a counter, probably in your database :
    • Your SQL query will look like update ad_counters set counter = counter + 1 where ad_id = 123
    • 123 being replaced by the identifier of your ad, of course
  • And, when displaying the ad, you'll have to select that counter, and display it alongside the ad.


For the second one, number of times an ad is clicked, the basic idea is generally :

  • Not have the ad be a direct link to the page of the product
  • Instead, the link of the ad will look like http://yoursite.com/ad.php?id=123
  • And, when someone load that page, it will :
    • increment the counter of clicks : update ad_clicks_counter set counter = counter + 1 where ad_id = 123
    • redirect the user to the real page of the ad, or display it directly.

In fact, this is precisely what's done on SO :

  • An ad has a link such as http://ads.stackoverflow.com/a.aspx?Task=Click&ZoneID=4&CampaignID=785&AdvertiserID=161&BannerID=1123&SiteID=1&RandomNumber=384213225&Keywords=php%2ccounter%2cx-user-highrep%2cx-user-registered
  • And when you click on it, you are redirected to the real page of the ad, which can be such as http://www.xpolog.com/home/solutions/landing.jsp


Of course, those two counters can be in the same table -- or even in the table in which you have the list of all ads :-)

Pascal MARTIN
thank you!! time to get to work :)I think I'll just add a counter field to my table and the "UPDATE SQL" on top of the ad.php script.
ggfan
You're welcome :-) Have fun !
Pascal MARTIN