hedgehog90 Posted January 6, 2011 Share Posted January 6, 2011 I may not be a PHP wizard, but a view counter seems pretty damn simple and easy to implement. Unless you leave it in my hands... It's a pretty simple problem, so I'm hoping it'll be a simple solution. Every time you load a game on my website, at the top of the php "playgame.php" is a function call: $objGlobal->update_gameplay($gameid); In my functions.class.php is the function declaration: function update_gameplay() { $args = func_get_args(); $getsql = mysql_query("select * from games where gameid = $args[0]"); if($getsql) { $resultgame = mysql_fetch_array($getsql, MYSQL_ASSOC); $timesplayed = $resultgame['timesplayed'] + 1; $updatesql = "update games set timesplayed = '$timesplayed', last_played = now() where gameid = $args[0]"; if(mysql_query($updatesql)) return true; else return false; } } Yet, when I load the playgame page, it adds 1,2 or 3!!! So this means the function is being called 1, 2 or 3 times, but how can that be if it is at the VERY top of the page? Try it yourself on my website, www.gpstudios.com. Click on a game and reload. For some reason, the newer games always add 1, but other older/more popular games add 1, 2 or 3 I've searched for the string "update" and the string "timesplayed" in every file using a program called FileSeek and I can say for certain that the function must be being called multiple times for some reason. What's weirder is that my game reviews (http://www.gpstudios.com/reviews.php) are also having their view counter incremented by 1 or 2 on a page load. Please help me solve this quick. Thanks, -Tom Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/ Share on other sites More sharing options...
Anti-Moronic Posted January 6, 2011 Share Posted January 6, 2011 Have you tried to modify sql to this: update games set timesplayed = timesplayed+1 Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1155626 Share on other sites More sharing options...
MMDE Posted January 6, 2011 Share Posted January 6, 2011 fetching of the game id is probably done earlier in the script too? not too much of the script that needs to run, especially those mysql queries. (see Anti-Moronic's post too) $objGlobal->update_gameplay($gameid); ^ here you give the gameid parameter you later in your function search for in the actual function, it expects no parameter... also, this would I not call a view counter. it just register the last time someone reloaded the page... (by looking at your update query) sorry if I've said something wrong (just trying to help) Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1155633 Share on other sites More sharing options...
Anti-Moronic Posted January 6, 2011 Share Posted January 6, 2011 MMDE is right. Even if my above tip works for you, you might want to reconsider the structure of this function (and perhaps this portion of the app in general). Esentially, you only need to run a single query and provide a single parameter. Should note however that if you do this you WILL have to ensure something expected is thrown into the sql query. You don't want the update query to run on every row because your WHERE clause was corrupt. Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1155643 Share on other sites More sharing options...
hedgehog90 Posted January 7, 2011 Author Share Posted January 7, 2011 Okay, i SHall implement update games set timesplayed = timesplayed+1 But I don't understand the rest of what you are saying. I paid people to create the website, but after looking through the files, I unerstand almost all of it now. When function update_gameplay is declared, it has no input parameters, but inside the function it does $args = func_get_args(); Which I believe is just a way of declaring a function with multiple inputs without having to declare the variables. Is this not a valid way of doing functions? because in my functions.class, func_get_args() is used everywhere... Also, none of this explains why the function is appearing to be called 1, 2 or 3 times. Thanks, hope you can explain this to me a little further. Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1156194 Share on other sites More sharing options...
Anti-Moronic Posted January 7, 2011 Share Posted January 7, 2011 Well using func_get_args() is just not good practice; it's lazy programming. You seen yourself the way you then have to refer to these parameters, AND you are allowing any number of parameters to be used. It isn't a 'huge' problem but it would be much better if the parameters were declared in the function. The fact that there is a 'functions.class' file tells me the app is not very well structured. Lots of people do this who don't know how to separate application logic. They include the file everywhere and that is how they access this global set of 'misc' functions. Sometimes the whole of the logic will be in this file. Again, not good practice. But aside from that, you have an immediate problem: the inconsistent incrementing. There is little we can do from the code you have offered here. This function as MMDE pointed out could have been called somewhere else. Only way to find that out is to look over the entire app. If you have timesplayed = timesplayed+1 there is absolutely no way that field will then be incremented more than once every time the query is run. This means if it IS then the query is being run more than once. Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1156197 Share on other sites More sharing options...
hedgehog90 Posted January 7, 2011 Author Share Posted January 7, 2011 Thanks for your reply. Could you explain how my functions.class file is bad practice? If I want to use functions on multiple pages, how else would I get them? More importantly though, I went to the playgame.php, and instead of calling the function, I just wrote: $updatesql = "update games set timesplayed = timesplayed+1, last_played = now() where gameid = $gameid"; mysql_query($updatesql); Predictably, this has done nothing. It still increments by 1 or 2. As I said earlier, I have searched all the php files for the update_gameplay function, and it is only ever called once, and I also searched all instances of update in a mysql query and none of them related to timesplayed. If I don't call the function at the top of the page though, then it doesn't increment at all... obviously. But this therefore means that the problem is with the top of the php document... All things are being done twice? Or the page is instantly reloading? I believe it might be the latter... but why? What should i search for now? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1156261 Share on other sites More sharing options...
Anti-Moronic Posted January 7, 2011 Share Posted January 7, 2011 Ahh I didn't say it was bad practice. It's just not good practice and far from best practice. This function update_gameplay()..why would that even be in a global functions file? It has no place. It should be localized to a 'game' object or class. I have to say, this is very strange behavior. Have you noticed it refresh? Do you have ajax which is maybe loading further content which includes this function? What you might want to do now is replace the query with an echo. Something like: echo (isset($_GET['test'])) ? "QUERY WAS HERE" : '' Then if the site is live and you have no dev environment you can put this in address bar to echo: mysite.com/gamepage/?test=true ..oh and although the update_gamplay function is only being called once, have you determined if this query is only being used once? Maybe it is being used somewhere else away from the function? Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1156294 Share on other sites More sharing options...
Anti-Moronic Posted January 7, 2011 Share Posted January 7, 2011 Oh, and what does this function call look like? Do you have the piece of code which calls this function padded with 10 or so lines either way. Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1156295 Share on other sites More sharing options...
hedgehog90 Posted January 7, 2011 Author Share Posted January 7, 2011 Function call is not padded. Even if it was though, why would that affect anything? I have ajax on the page for the comments and the rating, but as I have said before, I know that the function is not repeated anywhere I said in the last post I removed the function and just put the mysql query at the top of the playgame page, yet it still does double, so it must be reloading the page somewhere... I've tried the: echo (isset($_GET['test'])) ? "QUERY WAS HERE" : '' It doesn't identify anything new, just echos at the top of the page "QUERY WAS HERE". I'm pretty sure the page is just being loaded twice (or once or three times), very quickly. It's the only way to explain why the query is being done twice when outside of a function at the top of the playgame.php Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1156411 Share on other sites More sharing options...
Anti-Moronic Posted January 7, 2011 Share Posted January 7, 2011 Sorry, I mean we need to see the function call and the previous and next 10 or so lines surrounding it. As in, let's see the function call but not the entire script. On this 'refresh' theory, it is very very easy to see if that is the problem. You disable refresh in your browser or you just take note to see if the browser is being refreshed..I mean, that would be extremely obvious if it was that. Maybe somebody else can pitch in because I have absolutely no other ideas on how to debug this for you. The purpose of using ECHO "dfsafsda" is so that IF your ajax was then running this function it would likely output something. That is the only other thing I would take note of. If your ajax is requesting the same page or another page in your app which runs this function, this will happen. So, we now need to see 1) your function call, there may be some bad coding which is running the function twice for whatever reason, 2) your ajax calls, and what scripts/functions they are running. Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1156417 Share on other sites More sharing options...
hedgehog90 Posted January 7, 2011 Author Share Posted January 7, 2011 Hope this doesn't sound aggressive because you're been very helpful , but as I've said before, I have removed the function from the equation completely. This is what happens at the top of the page "playgame.php": <?php session_start(); require_once("inc/config.inc.php"); require_once("class/functions.class.php"); require('drawrating.php'); $objGlobal = new globalclass(); require_once("class/site_register.php"); $gameid = $_GET['gameid']; if(!empty($gameid)){ $objGlobal->update_gameplay($gameid); } $reviewlistings = $objGlobal->getgamereviews($gameid); $displaytitlesql = "SELECT * FROM games WHERE gameid = $gameid"; $displaytitle=$objGlobal->get_games($displaytitlesql); and so on... But, if I remove the function from functions.class and remove the line $objGlobal->update_gameplay($gameid); And replace it with $updatesql = "update games set timesplayed = timesplayed+1, last_played = now() where gameid = $gameid"; mysql_query($updatesql); No use of a function anywhere now, and I still get the bug. So now the top reads: <?php session_start(); require_once("inc/config.inc.php"); require_once("class/functions.class.php"); require('drawrating.php'); $objGlobal = new globalclass(); require_once("class/site_register.php"); $gameid = $_GET['gameid']; if(!empty($gameid)){ $updatesql = "update games set timesplayed = timesplayed+1, last_played = now() where gameid = $gameid"; mysql_query($updatesql); } $reviewlistings = $objGlobal->getgamereviews($gameid); $displaytitlesql = "SELECT * FROM games WHERE gameid = $gameid"; $displaytitle=$objGlobal->get_games($displaytitlesql); So, unless there is another mysql query being run somewhere else, like in an ajax function (which I have searched for already and found nothing) then it must mean the top of the page is being repeated. Get it? I could set up another database temporarily and refer to it in the playgame.php ONLY, and if it double increments there then that's surely proof. I'll just try that. Pretty certain it'll double increment there also. Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1156434 Share on other sites More sharing options...
hedgehog90 Posted January 7, 2011 Author Share Posted January 7, 2011 Yup, as I suspected. I replaced the query with the following $updatesql = "update arse set timesplayed = timesplayed+1 where gameid = 1"; mysql_query($updatesql); I set up a table called "arse" It's not mentioned anywhere else in my code obviously. And it does exactly the same incrementations as the other query. The top of the php file is being run once/twice/three times a lady.... for sure. Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1156439 Share on other sites More sharing options...
PaulRyan Posted January 8, 2011 Share Posted January 8, 2011 So you're saying that the older more popular games are incrementing by more than 1 on page reload? Has it occured to you that not just you may be loading that page? People might be playing the game as you are loading the page, as it is more popular... May not be the solution, just an observation of mine...totally uphelpful and off-topic. Regards, PaulRyan. Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1156486 Share on other sites More sharing options...
hedgehog90 Posted January 9, 2011 Author Share Posted January 9, 2011 That's not the case either PaulRyan. I wish it was though - would mean the games on my website would be getting thousands of plays a day Anyone else have some ideas? This is just bizarre... Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1157002 Share on other sites More sharing options...
Anti-Moronic Posted January 9, 2011 Share Posted January 9, 2011 I'll reply as a kind of bump and to add some more advice. Sadly, I have absolutely no idea what is going on here. Sometimes it proves very hard to debug problems when you're not working within the same environment as the problem. This is one of those cases. My first point of advice would be to hire somebody to debug this for you in your environment. I'm sure you could get somebody to take a close look without spending much money. That will surely allow you to focus your time on other things in the script... ..err..do you have access logs? Can you turn on some persistent logging? Then request the page, and take a look. See if it is indeed being requested twice. Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1157034 Share on other sites More sharing options...
hedgehog90 Posted January 10, 2011 Author Share Posted January 10, 2011 I've been searching the net and I have found several other forum topics on the same thing. Except with me it's only occasional, making it even harder to pin down. Lets agree on something - the query, despite only appearing on lines 12/13, is being done twice on the same page. Unfortunately, all the topics I've found don't have a definable solution. Most of them end with "I gave up" as is usually the case when forum hunting for a solution. This is undoubtedly a very strange bug I've got happening. Something else on the page must be interfering with it, but that could come from a HUGE range of places. Or maybe it's PHP/mySQL outdated techniques. I am an unfortunate patient with bizarre PHP related illnesses, and I need a PHP House MD to save me now!!! Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1157322 Share on other sites More sharing options...
hedgehog90 Posted January 11, 2011 Author Share Posted January 11, 2011 bump Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1157819 Share on other sites More sharing options...
hedgehog90 Posted January 14, 2011 Author Share Posted January 14, 2011 Why's no one replying? I really need to sort out this problem, I'll never give up. It's weird as hell... Please help. Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1159386 Share on other sites More sharing options...
Anti-Moronic Posted January 14, 2011 Share Posted January 14, 2011 "I'm sure you could get somebody to take a close look without spending much money" This is one of those times you have to ask yourself how much your time is worth? Is it worth the amount of time you've spent on it because a compenent developer could probably do it within an hour for $40. I am certainly *not* offering my services here, just saying. Try the freelance section or elance/rentacoder/freelancer. You may never resolve this on your own and you may never resolve this through the help of volunteers or people who don't even have access to your server. It's extremely hard and near impossible considering what we've covered to know what the problem is without access to the environment the app is being run in. That's why nobody is replying. Hope that helps, really do. Sorry I couldn't be of more help. Quote Link to comment https://forums.phpfreaks.com/topic/223561-simple-view-counter-adding-12-or-3/#findComment-1159451 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.