 |

vinco
User
Jun 20, 2008, 7:48 AM
Post #1 of 2
(544 views)
Shortcut
|
Is there any way to add a hit counter (number of time viewed) to news articles? Thank you for your help
|
|
|  |
 |

Dave
Staff
/ Moderator

Jun 20, 2008, 11:10 AM
Post #2 of 2
(524 views)
Shortcut
|
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 ?>
// 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
|
|
|  |
 | |  |
|