4

I have a .php script that counts the clicks of a button and places them into a .txt file, all fine here but what I have now only works on a single count. If, let's say, I make two buttons, it will show the same number of clicks for both of them.

I need the script to work foreach button separately...

PHP:

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

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

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

HTML:

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="submit" value="click me!" name="clicks">
</form>
<div>Click Count: <?php echo getClickCount(); ?></div>
2
  • Instead of writing the click count in a file, can't we just use one global variable? Commented Oct 30, 2014 at 9:39
  • 2
    a global variable would not work, since it only exists within the same request, so multiple requests will not update the number. Commented Oct 30, 2014 at 9:40

5 Answers 5

5

You should try different approach, I think. First, saving clicks in file is slower than in database, so you should use a database instead of file.

Then, you can create a table, with button_id field and corresponding click_number field. So if button with id="1" is clicked you increment click_number value for that button.

Sign up to request clarification or add additional context in comments.

4 Comments

Indeed, using a database would be a better solution than using a txt-file. The questioner should consider using databases.
Well, I guess I have no other choice than to use a database for my functions, I wanted to store the clicks counter in a root .txt file cause in the near future i will need to create an offline app that will place those clicks into a chart.
well if you actually want to improve you script you have to count the clics in JS and then store them. Using PHP for that is ridiculous, I thought this was some kind of class exercise.
@eagle1 Well, it's some sort of exercise since I'm new to php, I could do this in JS since I am used with front-end dev and then store it but I want to create the entire functions with php to get the hang of it and understand the differences. It's just my way of learning. Thanks for your contribution.
1

You can give the second button another name, and save the data in another file ?

Another possibility is to save all the button information in the same file, by using e.g. explode and implode

[edit: example]

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

function getClickCount($num=0)
{
    $clickinfo = explode(":",file_get_contents("clickit.txt"));
    return (int)$clickinfo[$num];
}

function incrementClickCount($num=0)
{
    $clickinfo = explode(":",file_get_contents("clickit.txt"));
    $clickinfo[$num]++;
    file_put_contents("clickit.txt", implode(":",$clickinfo));
}

5 Comments

Since I will have more than two buttons that will be involved (52 estimated) and saving data in root .txt files is slower, I don't think giving the button another name and saving it in another file would be the best solution. 52 buttons = 52 .txt files => pain in the server's behind. And to be honest, I can't quite picture how using implode/explode would work.
see my update for an example with implode/explode. Here the click data is stored into the text-file, separated with ":"
when testing, keep in mind to first put the counters at zero by putting the content of the file to "0:0:0:0" (with as many counters as you want)
Well, I did as mentioned but for some reason the counter won't start. And i did place the content of the file with 0:0 (testing with only two for now).
Keep in mind that the indexing starts at 0 and that I changed the post variables (clicks and buttonnumber), so you must make sure the function incrementClickCount is called. Here, it is working with incrementClickCount(1); !
0

if the two buttons call the same function:

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

then of course you'll get only one counter.

You can either make a button into another form that calls another function or do something like:

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="submit" value="click me!" name="clicks">
    <input type="submit" value="click me!" name="clicks2">
</form>
<div>Click Count: <?php echo getClickCount(); ?></div>

if( isset($_POST['clicks']) ) { 
    incrementClickCount("button1");
}
if( isset($_POST['clicks2']))
{
    incrementClickCount("button2");
}

function incrementClickCount($button)
{
if($button == "button1")
{
    $count = getClickCount() + 1;
    file_put_contents("clickit.txt", $count);
}
elseif($button = "button2")
{
 // other counter
}

}

Comments

0

You have to use session. first set session variable initial value 1 if it's not already initialized. After initialized, from 2nd click it will count +1 to every click, because it will satisfy the else statement

<?php
session_start();
?>
<html>
    <head></head>
    <body>
        <form method="post" action="submit.php">
            <input type="submit" name="count" value="Start counting" />
        </form>
        <?php
        if(isset($_POST['count'])){
            if(!($_SESSION['count'])){
                $_SESSION['count'] = 1;
            }else{
                $count = $_SESSION['count'] + 1;
                $_SESSION['count'] = $count;
            }
        }
        echo $_SESSION['count'];
        ?>
    </body>
</html>

Comments

-1

Save the button file as a css file with contents like this:

"Button","Value"
1,0
2,0
3,0
4,0 

PHP read function would explode on carriage return, and then loop through the data with a for-each loop and explode on comma. PHP write function would just rebuild the table with the updated value. easy peasy. You could use a regular for-loop too, if you wanted to hard-code in the 52 iterations.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.