Jump to content

automatically increasing numbers


unplugged_web
 Share

Recommended Posts

Is it possible to have a counter on a site increase the number by 53 approx. everyday. It doesn't have to suddenly increase by 53, but ideally increase by one every half hour. I guess I need to use JavaScript but don't know how to do that as I've only ever used very basic JavaScriptThanks in advance for any help.Lucy

Link to comment
Share on other sites

I would guess you are going to have to do something server-side too. What is the number for? You'll probably need to put it in a database. Difficult to say more without knowing what you're trying to achieve.
The counter is to show how many people have used a particular service. So when somebody uses the service then the number is then increased. It doesn't have to be 100% acurate although that would be better.Sorry I should have made the purpose of the counter clearer in my initial post.Thank you for your help.
Link to comment
Share on other sites

The counter is to show how many people have used a particular service.
The details will depend on what that service is, but I think you'll need an overall architecture that looks something like: (1) make the service log usage in some way, probably a database that you are going to access to get the number(2) decide when/how to get the number into the HTML pageIn all likelihood it will involve server-side scripting and a database, and, depending on your design for (1) above, may also involve client-side scripting.
Link to comment
Share on other sites

The details will depend on what that service is, but I think you'll need an overall architecture that looks something like: (1) make the service log usage in some way, probably a database that you are going to access to get the number(2) decide when/how to get the number into the HTML pageIn all likelihood it will involve server-side scripting and a database, and, depending on your design for (1) above, may also involve client-side scripting.
Thanks for that
Link to comment
Share on other sites

If you don't need the number to update without a page refresh, and it's solely a counter, you can do it with some pure PHP.Here's how it would be in PHP5 (for 4, you'd need to add the definition of file_put_cotnents()):

<?php$file = 'counter.txt';$count = file_get_contents($file);$count = $count + 1;file_put_contents($file, $count);echo $count;?>

The only thing this will output is a number based on how many times the page was accessed. A page refresh will make it 2, another one 3 and so on. The number is written is a file called "counter.txt" in the same folder as the PHP file.If you want to call this script automatically on certain times, you must ask the host if they have "cron jobs" and add this file as one if there are.If you want anything more sophisticated, then we must see how exactly is the service "triggered" so that the counter may only be appended at that event.By the way... you bastard! :) Providing false statistics, are we?

Link to comment
Share on other sites

If you don't need the number to update without a page refresh, and it's solely a counter, you can do it with some pure PHP.Here's how it would be in PHP5 (for 4, you'd need to add the definition of file_put_cotnents()):
<?php$file = 'counter.txt';$count = file_get_contents($file);$count = $count + 1;file_put_contents($file, $count);echo $count;?>

The only thing this will output is a number based on how many times the page was accessed. A page refresh will make it 2, another one 3 and so on. The number is written is a file called "counter.txt" in the same folder as the PHP file.If you want to call this script automatically on certain times, you must ask the host if they have "cron jobs" and add this file as one if there are.If you want anything more sophisticated, then we must see how exactly is the service "triggered" so that the counter may only be appended at that event.By the way... you bastard! :) Providing false statistics, are we?

Thanks for that, no the statistics don't have to been 100% acurate becasue they're just to give an idea of how much a service is used, so far it's been used nearly 1 million times.
Link to comment
Share on other sites

Sorry to sound stupid, but I didn't understand this bit:

for 4, you'd need to add the definition of file_put_contents()
I am using PHP4 and the moment, but would like to get this working before the server is upgraded.Also I want to start with 10 visitor is that possible.Also I didn't understand about the crons job - if I put the file (PHP or counter.txt?) in the crons job then it will automatically add a number at a certain time of day? And how would I get the contents of the counter.txt into the web page?Sorry to be a nuisance and thanks for your help
Link to comment
Share on other sites

A lot of useful PHP5 functions have equivalent PHP4 versions posted on their reference pages. If you check the comments on the reference page for file_put_contents you'll see this:

if ( !function_exists('file_put_contents') && !defined('FILE_APPEND') ) {define('FILE_APPEND', 1);function file_put_contents($n, $d, $flag = false) {	$mode = ($flag == FILE_APPEND || strtoupper($flag) == 'FILE_APPEND') ? 'a' : 'w';	$f = @fopen($n, $mode);	if ($f === false) {		return 0;	} else {		if (is_array($d)) $d = implode($d);		$bytes_written = fwrite($f, $d);		fclose($f);		return $bytes_written;	}}}

If you add that code to the page it will define file_put_contents so that you can use it under PHP4.

Link to comment
Share on other sites

Sorry to sound stupid, but I didn't understand this bit:I am using PHP4 and the moment, but would like to get this working before the server is upgraded.
Add the code justsomeguy posted right after the
<?php

line

Also I want to start with 10 visitor is that possible.
Sure. Just open the counter.txt file and replace the number. The counter will keep going on from that number on.
Also I didn't understand about the crons job - if I put the file (PHP or counter.txt?)
The PHP file.
in the crons job then it will automatically add a number at a certain time of day?
Yes. The exact interval is set on the cron job itself, provided your host provides this feature of course.
And how would I get the contents of the counter.txt into the web page?
There are a number of ways for that.If you have cron jobs, or at least are willing to manually run the increment script once in a while, you can get the current count by simply putting
<?php echo file_get_contents('counter.txt'); ?>

At the place where you want the counter to appear (provided the page with the counter is a PHP page of course).If you don't have cron jobs and yet want to increment the number automatically, you can just put all of the code I gave you (+ justsomeguy's addition) at the place where the number needs to appear. In that case though, the number will increase with each refresh, making the things quite dangerous (you'll reach 1 million and above sooner then you think if the server is as popular as you say).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...