Jump to content

Simple view counter adding 1,2 or 3?


hedgehog90

Recommended Posts

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

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

 

 

 

 

 

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!!!

Link to comment
Share on other sites

"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.

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.