Hit Counter
2 posts by 2 authors in: Forums > CMS Builder
Last Post: June 20, 2008 (RSS)
By vinco - June 20, 2008
Is there any way to add a hit counter (number of time viewed) to news articles?
Thank you for your help
Thank you for your help
Re: [vinco] Hit Counter
By Dave - June 20, 2008
No, we don't have a feature for that. But if you're comfortable editing code you can try the following:
- Open /lib/viewer_functions.php
- Make a backup copy of that file
- Add this to this to bottom just above ?>
Then create a field called 'hits' (or whatever you want to call it) add this in your page. This first field is the tablename, the second field is the fieldname, and the third field is the record number
Hope that helps.
- Open /lib/viewer_functions.php
- Make a backup copy of that file
- Add this to this to bottom just above ?>
// Note: This function is EXPERIMENTAL and may change in future
function incrementCounterField($tablename, $fieldname, $recordNumber) {
global $VIEWER_NAME;
// error checking
if (!$tablename) { die(__FUNCTION__ . ": No 'tablename' value specified!"); }
if (!$fieldname) { die(__FUNCTION__ . ": No 'fieldname' value specified!"); }
if (!$recordNumber) { die(__FUNCTION__ . ": No 'recordNumber' value specified!"); }
// update counter
$escapedTableName = mysql_real_escape_string(getTableNameWithPrefix($tablename));
$query = "UPDATE `$escapedTableName` SET `$fieldname` = `$fieldname` + 1";
$query .= " WHERE `num` = " .mysql_real_escape_string($recordNumber);
$result = @mysql_query($query);
if (!$result) { die(__FUNCTION__ . " MySQL Error: ". htmlspecialchars(mysql_error()) . "\n"); }
if (!mysql_affected_rows()) {
die(__FUNCTION__ . ": Couldn't find record '" .htmlspecialchars($recordNumber). "'!");
}
}
Then create a field called 'hits' (or whatever you want to call it) add this in your page. This first field is the tablename, the second field is the fieldname, and the third field is the record number
<?php incrementCounterField('news', 'hits', $newsRecord['num']); ?>
Hope that helps.
Dave Edis - Senior Developer
interactivetools.com
interactivetools.com