3

I found this script in php which counts button clicks and saves them to a txt file.

 <?php
    if( isset($_POST['clicks']) )
    { 
        clickInc();
    }
    function clickInc()
    {
        $count = ("clickcount.txt");

        $clicks = file($count);
        $clicks[0]++;

        $fp = fopen($count, "w") or die("Can't open file");
        fputs($fp, "$clicks[0]");
        fclose($fp);

        echo $clicks[0];
    }
    ?>

    <html>

        <head>

           <title>button count</title>

        </head>
        <body>
            <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
                <input type="submit" value="click me!" name="clicks">
            </form>

        </body>
    </html>

what i cant figure out is how to echo the number of button clicks to a different part of the html. i've tried placing:

 <?php
     echo $clicks[0];
 ?>

but that does not work. what am i doing wrong? thanks..

1
  • this looks so dirty - i know when you don't know the language this is pretty "difficult" but you should look at these php functions file_get_contents and file_put_contents and play with these to get more practice in php and working with files Commented Dec 10, 2013 at 8:32

2 Answers 2

1

I'd suggest separating the part of the code that reads the click count from the part that increments it, so that you can call each part on it's own. Then you don't have to save the click count from the actual incrementation part; you could get the click count on it's own whenever needed, exactly as it exists in the file at that point in time.

if( isset($_POST['clicks']) ) { 
    incrementClickCount();
}

function getClickCount()
{
    return (int)file_get_contents("clickcount.txt");
}

function incrementClickCount()
{
    $count = getClickCount() + 1;
    file_put_contents("clickcount.txt", $count);
}

With that, you could include the current count at any point in your HTML, by calling the getClickCount function.

    <div>Click Count: <?php echo getClickCount(); ?></div>
</body>
Sign up to request clarification or add additional context in comments.

1 Comment

@Atli where is the button code that will be clicked and the counter will be incremented? It just displays 0. How can we start the counter?
0

Because of your $clicks[0] is a part of clickInc function.

$clicsCount = 0
if( isset($_POST['clicks']) ) { 
    $clicsCount = clickInc();
}

function clickInc()
{
    $count = ("clickcount.txt");

    $clicks = file($count);
    $clicks[0]++;

    $fp = fopen($count, "w") or die("Can't open file");
    fputs($fp, "$clicks[0]");
    fclose($fp);

    return $clicks[0];
}

than put

<?php echo $clicsCount; ?>

where you need it

4 Comments

Note: this will only work after the button was clicked. A page view without increment will show 0.
Of cause, but this feature of an original script =) May be he want to show click counter after 1+ clicks.
thanks alot! that works. however, when ever i reload the page it says 0 clicks and only shows the actual number of clicks when the button is clicked. is there a way to show the actual number of clicks when loading the page?
Add function: showCurrentClicks() {return file_get_contents("clickcount.txt");} and use where you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.