RLAArtistry Posted October 25, 2012 Share Posted October 25, 2012 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. Quote Link to comment Share on other sites More sharing options...
requinix Posted October 25, 2012 Share Posted October 25, 2012 (edited) Track the products viewed in the session. Just their identifiers is enough, don't need the whole thing. If the current product isn't in there then increment the counter and add it to the list. Edited October 25, 2012 by requinix Quote Link to comment Share on other sites More sharing options...
RLAArtistry Posted October 25, 2012 Author Share Posted October 25, 2012 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? Quote Link to comment Share on other sites More sharing options...
akphidelt2007 Posted October 25, 2012 Share Posted October 25, 2012 I would create a separate table and just record all the details of each product view, like time, referer, ip address, session_ids, username, etc. Then you can do all sorts of analysis on your views including the counts you want. Quote Link to comment Share on other sites More sharing options...
RLAArtistry Posted October 25, 2012 Author Share Posted October 25, 2012 That is true akphidelt2007 but i didnt want to do another sql query for this. Seem a little extreme for such a small task. Quote Link to comment Share on other sites More sharing options...
requinix Posted October 25, 2012 Share Posted October 25, 2012 You might discover down the road that such information could be useful. If there's even the slightest chance of that then record the views in a database. Quote Link to comment Share on other sites More sharing options...
RLAArtistry Posted October 26, 2012 Author Share Posted October 26, 2012 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? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 26, 2012 Share Posted October 26, 2012 http://php.net/array_push Quote Link to comment Share on other sites More sharing options...
RLAArtistry Posted October 27, 2012 Author Share Posted October 27, 2012 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 Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 27, 2012 Share Posted October 27, 2012 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. 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. Quote Link to comment Share on other sites More sharing options...
RLAArtistry Posted October 27, 2012 Author Share Posted October 27, 2012 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 27, 2012 Share Posted October 27, 2012 (edited) 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 October 27, 2012 by Psycho Quote Link to comment Share on other sites More sharing options...
RLAArtistry Posted October 28, 2012 Author Share Posted October 28, 2012 Thank you guys for your help. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 28, 2012 Share Posted October 28, 2012 (edited) 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 October 28, 2012 by Christian F. Quote Link to comment Share on other sites More sharing options...
RLAArtistry Posted October 28, 2012 Author Share Posted October 28, 2012 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. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 28, 2012 Share Posted October 28, 2012 You're most welcome. Quote Link to comment 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.