CREATE A HIT COUNTER USING THE BUILT IN INCREMENTCOUNTERFIELD FUNCTION - Jul 24th, 2020


Using the built in incrementCounterField function, you can create a hit counter for a single record editor or a detail
page of a multi record editor like this:

In the section editor, create a text field called 'hits' (or whatever you want to call it). Then insert this code on
your viewer page.



<?php incrementCounterField('your_table_name', 'your_field_name', $your_table_name Record['num']); ?>



Or if you don’t want the counter to be incremented when the page is accessed by a particular IP Address (yours for
example), try this recommendation by Chris Waddell from Interactive Tools:



<?php if ($_SERVER['REMOTE_ADDR'] != '127.0.0.1') { incrementCounterField('your_table_name', 'your_field_name',
$your_tableRecord['num']); } ?>



Wherever on the viewer that you want to display the number of hits, insert this code as you would for any other field.



<?php echo $your_table_name['your_field_name '] ?>



LIST PAGE IMPLEMENTATION
For a counter on the list page of a multi-record editor, the process gets a bit more complex. This is because if you
used the approach above in a “foreach” loop, all the counters on all your records would increment each time the list
page was accessed.

Create a table called “Counter” or something similar.

Create a text field for each list page that you want to count. Name the fields something that will be meaningful,
preferably with the same name as the editor where you’ll be implementing the count.

Insert the record call to that table at the top of your viewer:



list($counterRecords, $counterMetaData) = getRecords(array(
'Table name' => 'counter',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$counterRecord = @$counterRecords[0]; // get first record



Then insert:



<?php incrementCounterField('counter', 'your_field_name', $counter Record['num']); ?>



Or again, if you don’t want the counter to be incremented when the page is accessed by a particular IP Address (yours
for example), try this version of Chris’s recommendation:



<?php if ($_SERVER['REMOTE_ADDR'] != '127.0.0.1') { incrementCounterField('counter' 'your_field_name',
$counterRecord['num']); } ?>



Into your viewer and:



<?php echo $counter['your_field_name '] ?>



Where you want the counter to appear.

As an added plus, you can enter any starting number in any of the counter fields and the count will begin from there, so
none of your pages need to show only a small number of hits.

GOING FURTHER
If you're using your counter to show how many times a particular record's detail page was viewed, you can insert your
counter display into your list page code and have it appear only if an admin has logged in.

Here's how:

Put this code after your load records calls to determine if an admin is logged in.


<?php $CMS_USER = getCurrentUserFromCMS(); ?>


Then, in the body of the viewer, where you want the counter to appear, use this:


<?php if( $CMS_USER['isAdmin'] && $record['your_field_name'] > 0): ?>This page was viewed <?php echo
$record['your_field_name'] ?> time<?php if($record[your_field_name'] == 1): ?><?php else :?>s<?php endif ?><?php endif
?>


If you wanted to decrement the counter, according to Chris Waddell from Interactive Tools:

There's nothing built-in, but you could copy the code, rename it, and change it to do what you want.

Here's an example. Call it with -1 as the fourth parameter to decrement a counter.


function incrementCounterFieldBy($tablename, $fieldname, $recordNumber, $amount = 1) {
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_escape(getTableNameWithPrefix($tablename));
$query = "UPDATE `$escapedTableName` SET `$fieldname` = IFNULL(`$fieldname`,0) + $amount";
$query .= " WHERE `num` = '" .mysql_escape($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). "'!");
}

}




The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service