I just added a feature to ChronicBabe.com, which runs a custom CMS, to keep track of how many times an article as been viewed. In the code that pulls an article from the database, this single line of SQL quickly increments a “hits” row (type=INT) in the “articles” table for a given id:
UPDATE articles SET hits=hits+1 WHERE article_id=$id
There is no need to fetch the existing number from the database, that’d be a wasted call. Instead, we just increment the existing count by one. Note that this method does not track visits by an actual person or differentiate a real person from a search engine hit (use a real stats program for that stuff). But, for a quick and dirty way of generating a “Top 10 Articles” kind of list on your website, this does the trick nicely.
Leave a comment