Jump to content

Best way to create a view counter?


slaterino

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.