Jump to content

Javascript / PHP Number Incrementer


Howdy_McGee

Recommended Posts

So I am trying to create something similar to the counter on this page (left side toward top): http://www.usagain.com/

 

I literally been playing around with all kinds of different methods all day but no luck. I first thought I could just write to a file and pull it no problem, but don't want to put a read limit on it (since the number can get as big as needed) so I used

filesize($myFile)

which I'm assuming is what is keeping it from overriding the first long of my file. Instead it just keeps going and I have an incremented number that is just stacking next to all the other numbers. So that went out the window.

 

I then though AJAX but then ran into the same problem above. Also learned that PHP variables have a scope like all languages ^.^    -  -  - Any suggestions on how to do this?

Link to comment
Share on other sites

I am trying to duplicate this: http://www.usagain.com/  <--- On the left of their page they have a counter. I've literally tried everything I can think of but they have something going on in their PHP that I do not. My code is fairly straightforward:

 

HTML/CSS/Javascript/AJAX

<html>
<head>
<title>Counter</title>
<script language="javascript" src="../jquery1.6.js"></script>
<script type="text/javascript">

function addCommas(nStr) //http://www.mredkj.com/javascript/nfbasic.html -- Source
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function getNum()
{
	$.post('test.php', function(data){
		$('#counter').html(addCommas(data));
	})
}	
setInterval(getNum, 1000);
</script>
<style type="text/css">
#counterContainer{color: #52504D;font-family:Verdana, Geneva, sans-serif;font-weight:bold;font-size:15px;position:relative;top:22px;}
#counter{color: #1E7EC8; font-size: 25px;letter-spacing:1px;}
</style>
</head>
<body onload="getNum()">
<div id="counterContainer">
	<div id="counter"><!--Counter Goes Here, Do Not Disturb--></div>
</div>
</body>
</html>

 

And my PHP just reads the number in the file, updates the number, ehcos it back to AJAX, and updates the file. Here's the code:

<?php
$fp = fopen("staticNum.txt", "r+");
flock($fp, LOCK_EX);
$num = fgets($fp, 11);
$num = intval($num)+1;

echo $num; 

fseek($fp, 0, SEEK_SET);
fputs($fp, "$num");
flock($fp, LOCK_UN);
fclose($fp);
?>

 

The problem is if 2 people view the page at the same time, the number will increment by 2 instead of 1. If 3 people view it the number will increment by 3 and so on. How do I prevent this and keep it incrementing by 1 no matter what?

Link to comment
Share on other sites

The problem with Javascript is that it won't keep track of the variable over time. As soon as the user refresher their page the number is reset to a static. I want to increment the number and also keep track of it as it gets bigger. I figured the easiest way to keep track of the variable would either be writing it to a file or database but because the user initiates the javascript, it gets called more than once, and written to the file more than once and therefore the number is incremented more than once (or so I'm thinking). I wasn't sure if there was a PHP function to prevent this or a better path to my solution :/

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.