Results 1 to 3 of 3

Thread: [RESOLVED] Link click counter

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2007
    Posts
    45

    Resolved [RESOLVED] Link click counter

    Hi, how can i make a link add 1 to a field in my database??

    In my db table i have 4 fields. ID, Link, Description and "number of clicks". How can I make "number of clicks" increase by 1 when i press on a link? Each link is printed from the Link field.

    PHP Code:
            echo "<tr>";
     
            echo 
    "<td class=tabell><img src=$row[2].jpg></td>";
            echo 
    "<td class=tabell>".$row[1]."<br><font color=red size=2>Antal klick: $row[3]</font></td>";
            echo 
    "<td class=tabell>".$row[0]."</td>";
            echo 
    "</tr>"
    $row[1] contains the whole link <a href....> in the db table and $row[3] (number of clicks) only shows "0" because I can't get it to increase when i click on the link.

    Or maybe this is totally wrong. Maybe i should build it all in another way?

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Link click counter

    First of all, never use echo to output HTML.
    PHP is an embedded script processing language; the PHP code should be placed within the HTML, not the other way round.

    As for a link counter: you will need to make the links point to a script that increments the count in the database and then redirects the user to their destination.
    If you want to count page views rather than specifically link activations then you should simply increment the counter on that page. Your host might also provide a tool that records these statistics without requiring you to write any code.

  3. #3
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    354

    Re: Link click counter

    I have used a technique where I had a webservice and I did an AJAX call to it incremented my counter then did the redirect to the site i.e. only 1 redirect. That is a bit harder than what penagate describes, it is an options.

    http://bisqwit.iki.fi/story/howto/phpajaxsoap/

    You will probably need to do more research than this is you want to do this method. However, it is a pretty good techique to learn, because you can leverage a web service to do a lot using javascript with little or no impact to the user.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width