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: [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: [rconring] Need to increment clicks counter

By Jason - September 6, 2012

Hi Ron,

The error was that I was missing a parentheses in my code (I've edited my previous post with the fix.).

It should have looked like this:

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

Note that this particular example assumes you have a variable in the URL called "recordNum".

There isn't a problem with the approach you took, as they're basically the same. I find that using mysql_get() or mysql_select when I don't need to retrieve uploads and pseudo fields keeps the code a little cleaner, but this is more of a styling choice than anything.

Glad everything is working now.
---------------------------------------------------
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 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