We help IT Professionals succeed at work.

PHP + MySQL Profile View Counter

stevefNYC
stevefNYC asked
on
322 Views
Last Modified: 2013-12-13
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
Comment
Watch Question

Joe WuDeveloper | Solutions Architect | CTO
CERTIFIED EXPERT

Commented:
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.

Author

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?
Developer | Solutions Architect | CTO
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.