Forum Moderators: coopster
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.
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;
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
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.