Need to increment clicks counter

5 posts by 2 authors in: Forums > CMS Builder
Last Post: September 6, 2012   (RSS)

The problem I have just seems so simple, yet it has me stumped even after having read similar posts on this forum and a lot of other PHP/SQL related articles.
I need to simply increment a counter in a record from a list of links to technical references each time a link to an external site is clicked.

The Data is as such:
list($techRecords, $linksMetaData) = getRecords(array(
'tableName' => 'links',
'loadUploads' => '0',
'allowSearch' => '0',
'where' => 'category = "3"',
));

The links are as such:
<?php foreach ($techRecords as $record): ?>
<a target="_blank" title="<?php echo $record['alt_text'] ?>" href="<?php echo $record['url'] ?>"><?php echo $record['title'] ?></a>
<br />
<?php endforeach; ?>

I just need to grab the record before leaving the page, increment $record['clicks'], put the record back. Is this not as simple as it looks?
Ron Conring
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987

Re: [rconring] Need to increment clicks counter

By Jason - September 4, 2012 - edited: September 6, 2012

Hi Ron,

No problem. There are a few ways of doing this. I think the easiest way would be to first send them along to a intermediate page (something like redirectPage.php). You would append the record number of the record you want to send them to to the end of the url.

In this page, you would first retrieve the record,

EXAMPLE:

$link = mysql_get("links", intval(@$_REQUEST['recordNum']));

You can then increment your counter using the incrementCountField function:

EXAMPLE:
incrementCounterField('links', 'clicks', $link['num']);

Finally, you can redirect the user to their destination link:

EXAMPLE:

redirectBrowserToURL($link['url']);
exit;


The user will probably not even notice they were redirected.

Hope this helps get you started.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Need to increment clicks counter

Thanks so much for you quick response. Although the example you gave me did not work as is, I got enough of an understanding of what needed to happen to get it to work another way. Code for clickCount.php:
---------------------------------------------------------
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
require_once "/hsphere/local/home/rconring/conring.com/cmsAdmin/lib/viewer_functions.php";
list($linkRecords, $linksMetaData) = getRecords(array(
'tableName' => 'links',
'loadUploads' => '0',
'allowSearch' => '0',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$linkRecord = @$linkRecords[0];

// $link = mysql_get("links", intval(@$_REQUEST['recordNum']));
incrementCounterField('links', 'clicks', $linkRecord['num']);
redirectBrowserToURL($linkRecord['url']);
exit
?>
----------------------------------------------------
I kept getting an error on the mysql_get line so I got the record as I would on a detail page. Can you tell me why I got the error and if any, what disadvantage my method has? Error was unexpected ";" in line ...... and I could not tell why.
Ron Conring
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987

Re: [Jason] Need to increment clicks counter

Thanks for your reply, Jason. I finally spotted the error and corrected it and now it works as it should.

You guys rock! Thanks again
Ron Conring
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987