Forum Moderators: coopster

Message Too Old, No Replies

Counting Clicks on links pulled from mysql

         

intriguedone

6:35 pm on Aug 19, 2007 (gmt 0)

10+ Year Member



My scenario:
I have a database with links to sites where people can use a search to search the database for certin criteria. The links meeting the criteria are displayed.

My question is, How would I go about counting the clicks, so that each time someone clicked on a link the counter went up one.

My display code.

while($row = mysql_fetch_array($sql)){
//Give values for mouseover function
$tut_notes=$row['tut_notes'];
$preview_popup=$row['preview'];
$path="<img src=\"images/$preview_popup\">";

// Show Results In table
echo "<tr>";
echo "<td>" . $row['tut_writer'] . "</td>";
echo "<td>" . "<a target=\"blank\" href=" . $row['tut_url'] . ">" . $row['tut_name'] . "</a>" . "</td>";

echo "<td>" . $row['tut_type'] . "</td>";
echo "<td>" . $row['art_name'] . "</td>";
echo "<td>" . $row['art_type'] . "</td>";
echo "<td>" . $row['art_comp'] . "</td>";

?>
<td> <a onmouseover="<?=$tt->show("$tut_notes")?>" href="#">Notes</a></td>
<td> <a onmouseover="<?=$tt->show("$path")?>" href="#">Preview</a></td>


<?php
echo "</tr>";

What I am not sure of is how to open the the link in a new window and maby refresh the page to add to the click counter for that specific link. Not sure if refreshing the page would work but it is the only thing I know of to pass the values and get the php function to work.

My thoughts were to add a column to the table "counter" and then get the row with that link in it, get the "counter" value and add +1 to the "counter" value.

Any suggestions of how to go about doing this or a good point in the right direction would be greatly appriciated.

cameraman

8:42 pm on Aug 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Instead of putting the url straight into the anchor, run it through a script to increment the count and then redirect to the url. Personally I'd use a table id number and then look up the target url in the counter script, but you could pass the url itself if you prefer.
<a target=\"blank\" href=\"counter.php?url=" . urlencode($row['tut_url']) . "\">" . $row['tut_name.....

Then in counter (and you probably already know this) the url comes to you via $_GET, so:
$target = $_GET['url'];

For security's sake you should do this after you've made the connection to mysql:
$safe = mysql_real_escape_string($target);
Then use the $safe variable in the sql.

After you bump the counter,
header("Location: $target\n");
exit;

intriguedone

10:07 pm on Aug 19, 2007 (gmt 0)

10+ Year Member



Thank you for the help.

I would actually prefer to use the table id number and then look up the target url but I am unsure how to get it from my search page that displays the output to the counter.php page.

I am guessing it has something to do with this line but not sure what I have to change to get the id
<a target=\"blank\" href=\"counter.php?url=" . urlencode($row['tut_url']) . "\">" . $row['tut_name.....

Obvoiusly I do not have alot of experience with PHP/MySql

cameraman

10:45 pm on Aug 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Instead of
<a target=\"blank\" href=\"counter.php?url=" . urlencode($row['tut_url']) . "\">" . $row['tut_name.....

you use
<a target=\"blank\" href=\"counter.php?id=" . $row['tut_id'] . "\">" . $row['tut_name.....

then in counter.php change
$safe = mysql_real_escape_string($target);
to
$id=intval($_GET['id']); // That is, if it's an integer

Use the id to bump the counter, then again to look up the url. Then use the url in header().

Note that I changed the name of the variable in the href, and then some more in counter.php, but there's nothing magical about any of it - it is what you say it is and what you want to use. I changed them so they stay descriptive of their purpose.

Also, since 'id' isn't fancy browser-confusing text anymore, it doesn't (likely) have to be urlencoded, and since you expect a number in the counter script, you can use intval instead of real_escape_string to make the value safe to use in a query.

intriguedone

11:24 pm on Aug 19, 2007 (gmt 0)

10+ Year Member



Thank you for all of your help.

I have it working seamlessly now.

cameraman

11:58 pm on Aug 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cool!

Now for your next trick...