Reputation: 1228
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>
Upvotes: 4
Views: 25632
Reputation: 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.
Upvotes: -1
Reputation: 59
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>
Upvotes: 0
Reputation: 753
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));
}
Upvotes: 1
Reputation: 810
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
}
}
Upvotes: 0
Reputation: 8467
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.
Upvotes: 5