Forum Moderators: coopster

Message Too Old, No Replies

Looking for a counting script

does not have to link to any page but rather just update a count by one

         

Shongololo

1:22 pm on May 28, 2003 (gmt 0)

10+ Year Member



I'm trying to setup a page what will allow users to keep a statistic by clicking on a button.

The button does not have to link to any page but rather just update a count by one when clicked.

Any ideas?

Thanks

jatar_k

3:45 pm on May 28, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld Shongololo,

How do you want to store the data? I see two options, in a file or in a db. I would think if you are just storing the number you should probably use a file.

The button would have a form action of itself, I would think, unless you want it to go somewhere else after they click. When they click you read the integer out of the file, add 1 and write the new number to the file (as well as displaying it probably).

I am going to give some php functions you may need since you didn't specify which language you have available.

fopen [php.net]
fwrite [php.net]
echo [php.net]

Shongololo

4:11 pm on May 28, 2003 (gmt 0)

10+ Year Member



hello jatar_k

a number and if possible a date if that does not get to complicated, I don't have any experience in this so please let me know what you think is the easiest to setup for a newbie

jatar_k

11:42 pm on May 28, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could have a little script at the top of the page to increment your counter.

You can fire it when they click the button by using a hidden field in the form.

Substitute "pagename.php" with the actual page name in the form below.

the form

<form action="pagename.php" method="post">
<input type="submit" value="add to counter">
<input type="hidden" name="action" value="add">
</form>

little update script

<?
if (isset($action) && $action == "add") {
$fp = fopen('counter.txt','r+');
$lastcount = fread($fp,1024);
$lastcount += 1;
rewind($fp);
fwrite($fp,$lascount);
fclose($fp);
echo "added 1 to counter";
}
?>

think that's right, it is just off the top of my head. Most of the functions can be found here
Filesystem Functions [ca.php.net]

The file counter.txt would need to be in the same folder as this page and would contain nothing but a number. I would suggest 1 to start. ;) The file would also have to be writable so "chmod 777".

The page would have to have a php extension and you would have to have php available on your server.

Any one can correct me or make a shorter/better one if they like. This is just a "push in the right direction".