Jump to content

Click count on <a href


Hrvoje

Recommended Posts

Hi there,

 

I have simple code with echo link on external page.

<?PHP
$sql="SELECT * FROM links WHERE link='external'";
if (!$q=mysql_query($sql))
{
echo "Error" . mysql_query();
die();
}
if (mysql_num_rows($q)==0)
{
echo "No data";
} else {

while ($row=mysql_fetch_array($q))
{
?>

<a href="<?PHP echo $row["link"];?>"><?PHP echo $row["link"];?></a>

<?PHP 
}
}
?>

In database I have field clicks, how to increase that field on every click for 1.

 

Thanks,

Hrvoje

Link to comment
Share on other sites

You can do this in one of two ways.

 

1. Use JavaScript to add an onclick function to the links to run a function that would make an AJAX call to a PHP page to increment the counter for the page being requested then allow the link to open the selected page.

 

2. Use an intermediary page. Instead of having your links open directly to the external page have them open to a page you host - passing a value to identify the external page. On that intermediary page, perform your Database query to update the hit count and then redirect to the external page.

 

Assuming you have a unique ID for your links, change your current page to something like this:

 

<?php
 
$query = "SELECT link FROM links WHERE link='external'";
$result = mysql_query($query)
if (!$result)
{
    echo "Error" . mysql_query();
    die();
}
 
if(!mysql_num_rows($result))
{
    echo "No data";
}
else
{
    while ($row = mysql_fetch_assoc($result))
    {
        echo "<a href='referrer.php?link={$row['link']}'>{$row['link']}</a>\n";
    }
}
 
?>

 

 

<?php
 
//Get value from URL and update count
$link = mysql_real_escape_string($_GET['id']);
$query = "UPDATE link SET count = count + 1 WHERE link='$link'";
$result = mysql_query($query);
 
if (!$result || !mysql_num_rows($result))
{
    echo "Error" . mysql_query();
    die();
}
 
//Redirect user to external link
header("Location: {$link}");
exit();
 
?>
Link to comment
Share on other sites

I had originally wrote the code with the assumption that you had an ID column in your database and then decided to change it to only use the link value. In doing so, I forgot to revert a line to the correct value

 

This

 

$link = mysql_real_escape_string($_GET['id']);

 

Should be this

 

$link = mysql_real_escape_string($_GET['link']);

 

Also, in case it isn't clear. The second block of code in my first reply would be for the page referrer.php that you would create for the links to point to.

Link to comment
Share on other sites

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.