Need to increment clicks counter

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

By rconring - September 4, 2012

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: [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

By rconring - September 6, 2012

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