slaterino Posted July 10, 2009 Share Posted July 10, 2009 What's the best way to create a view counter? By this, I mean when everytime someone views a page it adds 1 to a field in a table. How would I do this? I'm just looking for a simple solution really. Thanks Russ Quote Link to comment https://forums.phpfreaks.com/topic/165435-best-way-to-create-a-view-counter/ Share on other sites More sharing options...
kickstart Posted July 10, 2009 Share Posted July 10, 2009 Hi Possibly easiest to have a table keyed on the page name with a count:- CREATE TABLE `test`.`CountTable` ( `PageName` VARCHAR( 50 ) NOT NULL , `ViewCount` INT NOT NULL , PRIMARY KEY ( `PageName` ) ) Then do:- INSERT INTO CountTable (`PageName`,`ViewCount`) VALUES ('Some Page Name',1) ON DUPLICATE KEY UPDATE `ViewCount`=`ViewCount`+1 'Some Page Name' could just be a constant and individually in each page (not nice) or use $_SERVER['PHP_SELF'] and just put it in an include at the top of all the pages you are interested in:- $sql = "INSERT INTO CountTable (`PageName`,`ViewCount`) VALUES ('".$_SERVER['PHP_SELF']."',1) ON DUPLICATE KEY UPDATE `ViewCount`=`ViewCount`+1"; All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/165435-best-way-to-create-a-view-counter/#findComment-872683 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.