Link to home
Start Free TrialLog in
Avatar of Panos
PanosFlag for Germany

asked on

Coldfusion cfcookie question

Hello experts.
I'm using the jquery rating code(http://orkans-tmp.22web.net/star_rating/).
I'm passing the id of the rated item to a component where i'm storing the rate of one artikel in DB and set  a cookie like:
<CFCOOKIE name="artrate-#arguments.id#" value="#arguments.rate#" expires="never">
so i have for a rating with value 5 for my artikel with id 27 the cookie artrate-27   value 5.

The question is how can i check if i have rated one artikel and get the value?
i can have several cookies looking like
artrate-27
artrate-34 .....
Any help?
SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Panos

ASKER

hi agx.
How can i have a list with different values for each cookie?
You'd need to use nested lists.  Create a pair for each rating articleID|rating  (delimited by "|"), then append other ratings with a comma

ie articleID|rating,articleID|rating,articleID|rating

Another approach that might be simpler is using json. You could create a small structure that stores the article id and rating.  Serialize it and store it as a cookie.  

<cfset articleID = 27>
<cfset rate = 5>

<!--- store values in cookie --->
<cfset ratings = {} >
<cfset ratings[articleID] = rate>
<CFCOOKIE name="artrate" value="#serializeJSON(s)#" expires="never">

When you need to read / search the ratings, just deserialize the cookie and search the structure as usual.

<cfif structKeyExists(COOKIE, "artRate")>
       <!--- deserialize cookie value back into a structure --->
      <cfset ratings = deserializeJSON(COOKIE.artRate)>
        <cfoutput>
      <!--- search for article rating --->
      <cfif structKeyExists(ratings, articleID)>
            found rating for articleID = #articleID# rating = #ratings[articleID]#
      <cfelse>
            no rating found for articleID = #articleID#
      </cfif>
        </cfoutput>
</cfif>
Avatar of Panos

ASKER

agx i get an error:
Variable S is undefined.
Sorry, that's a typo.  This line
    <CFCOOKIE name="artrate" value="#serializeJSON(s)#" expires="never">

Should be:

 <CFCOOKIE name="artrate" value="#serializeJSON(ratings)#" expires="never">
Avatar of Panos

ASKER


I tried to use twice the code with different settings for articleId and rate.
After that i changed the code in the top to:
<cfparam name="url.art_ID" default="">
<!--- <cfset articleID = 30>
<cfset rate = 5> --->
<cfset articleID = #url.art_id#>
<!--- store values in cookie --->
<!--- <cfset ratings = {} >
<cfset ratings[articleID] = rate>
<CFCOOKIE name="artrate" value="#serializeJSON(ratings)#" expires="never">
 --->
I did check the function using the values in the url but i get a value for rate only for the last one.It seems that the second cookie is not been added to the first one.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Panos

ASKER

very good agx.
it is working perfect.
thank you very much
Avatar of Panos

ASKER

Thank you again
regards
panos