views:

33

answers:

1

Looking for a way to track the last 5 product ids in a cookie via coldfusion. I'll set a new id each time a product page is visited. The product ids could be stored in a comma separate list. How would I store only the last 5 product ids?

+1  A: 

You could try something like this (untested):

<cfset thisproductid = id_you_somehow_know_from_this_page>
<cfif not structkeyexists( cookie, 'mylist' )>
    <!--- no cookie? make one and set it to this ID --->
    <cfcookie name="mylist" value="#thisproductid#">
<cfelse>
    <cfif listlen( cookie.mylist ) eq 5>
        <!--- lifo --->
        <cfset cookie.mylist = listdeleteat( cookie.mylist, 1 )>
    <cfelse>
        <!--- check for odd conditions like listlen gt 5? up to you --->
    </cfif>
    <cfset cookie.mylist = listappend( cookie.mylist, thisproductid )>
</cfif>
Ken Redler