Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 27

Thread: click counter when link is pressed

  1. #11
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    I can't figure out how ?click=yes would help with the independence, but I have two ideas for that.

    Doesn't each profile have its own database entry? Can't you just put the profile's visit count in that entry?

    Otherwise, the text file needs to store which user each number applies to. This could be done by either changing the text put in the file or using a different file for a different user.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  2. #12
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    Save it as "test.php", not html

  3. #13
    Join Date
    Apr 2007
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Yes, each profile has its own mysql database entry. But I don't want to use the database for the link counter. I would rather just have a unique link for every person's profile. And then somehow make the number go up when the link is pressed. I almost have it done with the code that was posted earlier in this thread, but now I need it to work on many different profile pages. Not just one.

    Thanks again for any help.

  4. #14
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    I don't understand that, but anyway... You're going to be dependent on the database however you do it. I think the database would be the easiest route (for the server), but the second-easiest would be a modified filename. The filename will depend on the user's ID which will come from the DB.
    Code:
    <?php
    $id = // the user's ID
    $file = $id . '.txt';
    if(!file_exists($file)){
      file_put_contents($file, '0');
    }
    if($_GET['click'] == 'yes'){
      file_put_contents($file, ((int) file_get_contents($file)) + 1);
      header('Location: ' . $_SERVER['SCRIPT_NAME']);
      die;
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
      <title>counter example</title>
    </head>
    <body>
      <h1><?php echo file_get_contents($file); ?></h1>
      <a href="?click=yes">clickMe</a>
    </body>
    </html>
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  5. #15
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    I believe this is just what I need but as a newbie, I have a dumb question. Where does the link reside that the user would click?

    Thanks
    WLL

    Quote Originally Posted by Nyne Lyvez View Post
    I'm not real good with PHP, but I wrote a small javascript that will do what you're asking for...

    Code:
    <HTML>
     <HEAD>
      <TITLE> Clicker Counter Thingamajig </TITLE>
    </head>
    
    <body>
    
    
    <script type="text/javascript">
    
    var clicks = 0;
    function linkClick(){
        document.getElementById('clicked').value = ++clicks;
    }
    
    document.write('<a href="#" onclick="linkClick()">Click Me!</a>');
    
    
    </script>
    
    You have clicked the link <input id="clicked" size="3" onfocus="this.blur();" value="0" > times.
    
    
    
     </BODY>
    </HTML>

  6. #16
    Join Date
    Jan 2006
    Location
    Ft. Smith, AR
    Posts
    795
    Thanks
    57
    Thanked 129 Times in 116 Posts

    Default

    Code:
    document.write('<a href="#" onclick="linkClick()">Click Me!</a>');
    That is the link written by javascript... However, I would recommend using one of the version the other guys posted, as I am definitely not very good at scripting. I'm more of a css kinda guy.
    --------------------------------------------------
    Reviews, Interviews, Tutorials, and STUFF
    --------------------------------------------------
    Home of the SexyBookmarks WordPress plugin

  7. #17
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Does the click count need to span responses? If User A clicks the link and leaves the page, and User B does the same, should the count be 1 or 2? If User A follows the link twice but in two different loads of the page, should the count be 1 or 2?

    If you answered 1 the first question, Nyne's script will do it. The link will appear in place of the script tag.

    If you answered 2 to both questions, you need to use a simple database or other file, no cookies required. Both techietim's and my own script implement this.

    If you answered 2 and 1, you need to use cookies to record either 0 or 1 (a Boolean) click for each user and add them together via a database.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  8. #18
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I believe small javascript function is just what I need but as a newbie, I have a dumb question. Where does the link reside that the user would click?

    Thanks
    WLL

  9. #19
    Join Date
    Jan 2006
    Location
    Ft. Smith, AR
    Posts
    795
    Thanks
    57
    Thanked 129 Times in 116 Posts

    Default

    Code:
    <HTML>
     <HEAD>
      <TITLE> Clicker Counter Thingamajig </TITLE>
    </head>
    
    <body>
    
    
    <script type="text/javascript">
    
    var clicks = 0;
    function linkClick(){
        document.getElementById('clicked').value = ++clicks;
    }
    
    document.write('<a href="#" onclick="linkClick()">Click Me!</a>');
    
    
    </script>
    
    You have clicked the link <input id="clicked" size="3" onfocus="this.blur();" value="0" > times.
    
    
    
     </BODY>
    </HTML>
    The highlighted section is the link
    --------------------------------------------------
    Reviews, Interviews, Tutorials, and STUFF
    --------------------------------------------------
    Home of the SexyBookmarks WordPress plugin

  10. #20
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Here's a simplified version of Nyne's script, where the link is not generated or changed by JavaScript; it will look exactly as below. If you still don't get something please specify what it is.
    Code:
    <HTML>
     <HEAD>
      <TITLE> Clicker Counter Thingamajig </TITLE>
      <script type="text/javascript">
        var clicks = 0;
        function linkClick(){
          document.getElementById('clicked').value = ++clicks;
        }
      </script>
    </head>
    
    <body>
    
    
    <a href="#" onclick="linkClick()">Click Me!</a>
    
    You have clicked the link <input id="clicked" size="3" onfocus="this.blur();" value="0" > times.
    
    
    
     </BODY>
    </HTML>
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

Bookmarks

Posting Permissions

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