Note that there are some explanatory texts on larger screens.

plurals
  1. POCookie array for Recently Viewed - need to extract data from array and cap cookie to 5 IDs
    primarykey
    data
    text
    <p>I am trying to create a recently viewed feature to a website. The idea is that you have a box in the right nav that shows your last 3 products viewed. You don't need to be logged it and it's no problem if the user clears cookies, it just starts over.</p> <p>From what I've researched, the best way to do this is through a cookie array (as opposed to setting 5 cookies or keep adding cookies or doing something in mysql).</p> <p>I'm having 2 problems:</p> <ol> <li>First, the array keeps adding values, I want it to cap it at 3 values and from there drop the oldest, then add the newest. So, if you visited 7 product page IDs in this order: 100,200,300,400,500,600,150, the cookie should store the values (500,600,150). The first is the oldest of the 3, the last is the newest.</li> <li>Second, I'm unclear of how to extract the array into something usable. The array is of ID numbers that I guess I need to query against the DB.</li> </ol> <p>When I put this on the page:</p> <pre><code>COOKIE: &lt;?php echo $cookie; ?&gt; </code></pre> <p>I get this:</p> <pre><code>COOKIE: a:7:i:0;s:3:"100";i:1;s:3:"200";i:2;s:3:"300";i:3;s:3:"400";i:4;s:3:"500";i:5;s:3:"600";i:6;s:3:"150";} </code></pre> <p>This is my code:</p> <pre><code>//set product id $product_id = [//some stuff here sets the product id] // if the cookie exists, read it and unserialize it. If not, create a blank array if(array_key_exists('recentviews', $_COOKIE)) { $cookie = $_COOKIE['recentviews']; $cookie = unserialize($cookie); } else { $cookie = array(); } // add the value to the array and serialize $cookie[] = $product_id; $cookie = serialize($cookie); // save the cookie setcookie('recentviews', $cookie, time()+3600); </code></pre> <p>How do I first get the cookie to hold 3 values and drop the oldest? What is the best way to extract those IDs into something I can put into a query?....str_replace?</p> <p>This brings up another question, which is should I just put the URL, anchor text, and a couple of attributes of the product into the cookie and not look it up with php/mysql?</p> <p>As always, thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload