Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

PHP + MySQL Profile View Counter

Hi all,

I'm working on adding profile view counters for the following: http://www.f1autoimports.com/inventory

I'm having a bit of difficulty figuring out what kind of algorithm to use so that only one counter hit per profile is registered with a user session. I think it makes sense for a users hit to increment the counter on multiple profiles, but only one per session.

Anyway, I'm looking for an elegant solution/algorithm/ideas. I already have a counter field in my table ready to go which defaults to zero for each new registered profile. I'd like to also leave the mathematics to MySQL, versus PHP.

Thank you kindly for your help.

- sf
0
stevefNYC
Asked:
stevefNYC
  • 2
1 Solution
 
nizsmoDeveloperCommented:
If I understand you correctly, you are wanting to only increment the counter (by 1) when the user views a profile for the FIRST time (for that session), but preventing the counter from incrementing any more even if the user visits the same profile again within the same session? But the user can view other profiles that they have not viewed in this session before, and it would still increment those corresponding counters?

If this is what you are looking for, then I would go about it like this: create a array session variable, and store all the unique identifiers of the profiles (profile id??) into the array, one by one as the users view each different profile, this way we can tell what profile the user has already viewed in the particular session.

Everytime the user views a profile, the code checks to see if the profile id is present in the array, if it is then the user has visited the profile in this session before already, therefore does not increment the counter, otherwise increment the counter by 1.

A simple function to check whether some value is in the array:

function searchlist($value,$alist)
      {
            if($alist == '')
                  return false;

            foreach ($alist as $avalue)
            {
                  if($avalue==$value)
                        return true;

            }
            return false;
      }

This will simply return true if the given value is in the given array ($alist) otherwise returns false if not.

Let me know how you go.
0
 
stevefNYCAuthor Commented:
Well, that sounds like a reasonable solution.

I have to first see if my PHP framework allows for this type of session registration. I'm not entirely sure I can use an array for values for a particular session variable.

Is handling a counter like this silly either way?
0
 
nizsmoDeveloperCommented:
I think it is reasonable to do this, since it gives you a more accurate counter value, and prevents abuse in regards to hitting refresh (F5) multiple times by any user which will then give the counter an inaccurate value.

Not sure what you mean by your PHP framework allowing session registration, however an array session variable can simply be created and used just as a normal array variable in PHP:

$somearray[0] = "value1";
$somearray[1] = "value2";

$_SESSION["someSessionArray"] = $somearray;

echo $_SESSION["someSessionArray"][1]; // This should echo value2


Please let me know how you get on.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now