Jump to content

Product Viewed Counter


RLAArtistry

Recommended Posts

Hey guys, its me again.

I want to add a counter for every time a product is viewed (this counter is stored in the same database table as the product itself). That way I can generate a most viewed product list.

 

I added this bit of code

UPDATE products SET viewCount=viewCount+1 WHERE pid='$pid

The problem is, every time the page refresh the counter adds 1, how can I make it add 1 per session without having to save the session id to my database?

 

Thank you, any suggestion will be appreciated.

Link to comment
Share on other sites

Let me see if I understood you correctly.

 

1. Create like a session array

2. Store viewed products ID in that array

3. when someone click on a product, the script will check if the current product id is already in the array

4. If not add 1 to the database.

 

Is that what you're suggesting?

Link to comment
Share on other sites

I totally get what you guys are saying but I only need the views. The views I will store in a database. What I don't need is the session id, ip or anything else.

 

Now, I am not that knowledgeable about the push array method. Can anyone help me with that?

Link to comment
Share on other sites

Thank you Christian F. but I've already read up on it and realized it wasnt recommended. Therefore I had a change of plans and came up with this bit of code.

 

 
$currentproduct = $_GET[[color="#800000"]'pid'[/color]]; 
$_SESSION[[color="#800000"]'productID'[/color]][] = $currentproduct; 
[color="#00008b"]foreach[/color]($_SESSION[[color="#800000"][color="#800000"]'productID'[/color][/color]] [color="#00008b"]as[/color] $key=>$value) { 
[indent=1]echo [color="#800000"]'The value of $_SESSION['[/color].[color="#800000"]"'"[/color].$key.[color="#800000"]"'"[/color].[color="#800000"]'] is '[/color].[color="#800000"]"'"[/color].$value.[color="#800000"]"'"[/color].[color="#800000"]' <br />'[/color]; } [/indent]

 

This code save my product viewed in an array. Now I need help with a code that will check the array to see if the current pid exist, if it does or if it is greater than 1 then it wont update the views counter.

I tried

array_count_value

to see if the value exist greater than 1, but keep getting an error.

 

Basically I need a code that will check the array and if the the current product pid exist it will update or not update the views.

 

Can someone please help me.

 

Thanks in advance

Link to comment
Share on other sites

Glad to hear that. Though, is it you or the forum software who added all of that BBcode inside the code tags? In either case, I would recommend turning off the Rich Text editor (top left button) whenever pasting code; It's unfortunately not quite up to snuff yet.

 

Anyway, after removing the extraneous BBcode, this is how your code looks like:

$currentproduct = $_GET['pid'];
$_SESSION['productID'][] = $currentproduct;
foreach ($_SESSION['productID'] as $key => $value) {
echo 'The value of $_SESSION[' . "'" . $key . "'" . '] is ' . "'" . $value . "'" . ' <br />';
}

 

Besides the fact that the echo could have been done a bit cleaner, by using double quotes around it, there isn't really any difference between this and array_push (). The only difference is that by using your code, your pushing an element onto the end of the array, instead of the start. This manner is slightly more efficient, but not something that should be a real concern before profiling have determined it to be an actual issue.

 

Now, I don't know how your other code looks like, but the above snippet doesn't take into account already existing IDs in the array. So I'm just going to assume that you don't have it. :P

In any case, I'd solve this issue by saving the ID as the index, and then checking whether or not the index has been set for each page. If not, then update the database and add the index with the value TRUE to the session array. Like this:

$PID = intval ($_GET['pid']);
if (!isset ($_SESSION['items_viewed'][$PID])) {
   $_SESSION['items_viewed'][$PID] = true;
   // Update database.
}

 

No need to check the number of anything, as long as the ID is set then you know it has been recorded already. ;)

Link to comment
Share on other sites

I am not sure what happened to my coding. I did noticed however, that I didn't get the usual popup when I clicked on the code icon, just got the tags, figured it was because I was using a tablet.

 

The code you posted worked perfectly, you are a genius Christian F., but I have 1 question.

 

I like to get an understanding of a code so I can figure the thought process behind it. Why did you use "intval"? The code seem to work without it.

Link to comment
Share on other sites

In any case, I'd solve this issue by saving the ID as the index, and then checking whether or not the index has been set for each page. If not, then update the database and add the index with the value TRUE to the session array. Like this:

$PID = intval ($_GET['pid']);
if (!isset ($_SESSION['items_viewed'][$PID])) {
   $_SESSION['items_viewed'][$PID] = true;
   // Update database.
}

 

Why not just store the IDs as values instead of making then indexes and creating a more complicated mufti-dimensional array with a bunch unnecessary "true" values?

$PID = intval ($_GET['pid']);
if(!isset($_SESSION['items_viewed']) || !in_array($PID, $_SESSION['items_viewed']))
{
   $_SESSION['items_viewed'][] = $PID;
   //Update database
}

 

Note; the isset() check would not be needed if that session variable is created when the user first accesses the site or logs in for instance.

Edited by Psycho
Link to comment
Share on other sites

Psycho: The main reason is to cut down on the amount of code, and to avoid having to use in_array () to search through the array. Other than that there is no difference in our code, as we both have a multi-dimensional array (with an "unnecessary" index in your case, and a "true" value in mine).

We also have the added bonus of isset () being much faster, thought that's not much of a concern in this case. :)

 

RLA: We used intval () as a part of the input validation/sanitation, to ensure that we are indeed getting a numerical ID and nothing else. Never trust the user, after all.

You can also add a check for $PID == 0, and skip the whole updating, since a product should never have an ID of 0; Seeing as AUTO_INCREMENTS starts at 1. Just a little tip. ;)

Edited by Christian F.
Link to comment
Share on other sites

RLA: We used intval () as a part of the input validation/sanitation, to ensure that we are indeed getting a numerical ID and nothing else. Never trust the user, after all.

You can also add a check for $PID == 0, and skip the whole updating, since a product should never have an ID of 0; Seeing as AUTO_INCREMENTS starts at 1. Just a little tip. ;)

Christian F.

Thank you very much.

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.