Howdy_McGee Posted January 4, 2012 Share Posted January 4, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/254370-javascript-php-number-incrementer/ Share on other sites More sharing options...
requinix Posted January 5, 2012 Share Posted January 5, 2012 What are you counting? At (about) what rate does it normally increase? Quote Link to comment https://forums.phpfreaks.com/topic/254370-javascript-php-number-incrementer/#findComment-1304374 Share on other sites More sharing options...
Howdy_McGee Posted January 6, 2012 Author Share Posted January 6, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/254370-javascript-php-number-incrementer/#findComment-1304918 Share on other sites More sharing options...
Howdy_McGee Posted January 6, 2012 Author Share Posted January 6, 2012 I wan the number to increment by 1 every second no matter what. :S didn't realize I posted this 2 days ago sorry mods! Some suggestions were cron jobs or linux timestamps(?) and cron only goes by minute from my understanding not seconds. So that's kinda out the window. Quote Link to comment https://forums.phpfreaks.com/topic/254370-javascript-php-number-incrementer/#findComment-1304934 Share on other sites More sharing options...
requinix Posted January 6, 2012 Share Posted January 6, 2012 One per second? See, this is why I asked and didn't just give you a blind answer. You don't need AJAX for that. Pure JavaScript will be fine. Set an interval for every one second, and when your function fires increase the counter. That's all. Quote Link to comment https://forums.phpfreaks.com/topic/254370-javascript-php-number-incrementer/#findComment-1304999 Share on other sites More sharing options...
Howdy_McGee Posted January 6, 2012 Author Share Posted January 6, 2012 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 :/ Quote Link to comment https://forums.phpfreaks.com/topic/254370-javascript-php-number-incrementer/#findComment-1305007 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.