Coldfusion Cookie Issues

Discussion in 'Programming' started by gavy, Aug 1, 2009.

  1. #1
    Hi Guys! I am trying to create a cookie and if the cookie exosts, expand that cookie with other values.

    Well here is the code what i am trying todo:

    
    <cfscript>
    function listRemoveDuplicates(lst) {
    var i = 0;
    var delim = ",";
    var asArray = "";
    var set = StructNew();
    
    if (ArrayLen(arguments) gt 1) delim = arguments[2];
    
    asArray = ListToArray(lst, delim);
    for (i = 1; i LTE ArrayLen(asArray); i = i + 1) set[asArray[i]] = "";
    
    return structKeyList(set, delim);
    }
    </cfscript>
    <cfset favlist = #ID# & "&" & #catname#>
    <cfif NOT isDefined('cookie.favorites')>
    	<cfcookie name="favorites" value="#favlist#">
    <cfelse>
    	<cfcookie name="favorites" value="#listRemoveDuplicates(ListAppend(favorites,'#favlist#',','))#">    
    </cfif>
    
    Code (markup):
    The above code is just adding the ID and catname in cookie variable

    favorites as ID & catname and when i check the values in the cookie, it shows me something like this:

    favorite = 160&94, 123&66, 122&88

    Okay Now in next page i just try to simply the Cookie by splitting.

    I am trying to make that for every cookie. i should set up a value when one should contain like:

    ID = 160

    catName = 94

    so that i can use them in my link to make a favorite link.

    i am trying something like below:

    first i am converting the list to array and then running a loop over array to see its results:

    results appear like:

    160&94
    123&69

    how do i furthur Split it to two variables and furthur use it:

    Here is my little try:

    
    <cfscript>
    function split(str) {
        var results = arrayNew(1);
        var splitstr = ",";
        var treatsplitstrasstr = true;
        var special_char_list = "\,+,*,?,.,[,],^,$,(,),{,},|,-,&";
        var esc_special_char_list = "\\,\+,\*,\?,\.,\[,\],\^,\$,\(,\),\{,\},\|,\-";    
        var regex = ",";
        var test = "";
        var pos = 0;
        var oldpos = 1;
    
        if(ArrayLen(arguments) GTE 2){
            splitstr = arguments[2]; //If a split string was passed, then use it.
        }
        
        regex = ReplaceList(splitstr, special_char_list, esc_special_char_list);
        
        if(ArrayLen(arguments) GTE 3 and isboolean(arguments[3])){
            treatsplitstrasstr = arguments[3]; //If a split string method was passed, then use it.
            if(not treatsplitstrasstr){
                pos = len(splitstr) - 1;
                while(pos GTE 1){
                    splitstr = mid(splitstr,1,pos) & "_Separator_" & mid(splitstr,pos+1,len(splitstr) - (pos));
                    pos = pos - 1;
                }
                splitstr = ReplaceList(splitstr, special_char_list, esc_special_char_list);
                splitstr = Replace(splitstr, "_Separator_", "|", "ALL");
                regex = splitstr;
            }
        }
        test = REFind(regex,str,1,1);
        pos = test.pos[1];
    
        if(not pos){
            arrayAppend(results,str);
            return results;
        }
    
        while(pos gt 0) {
            arrayAppend(results,mid(str,oldpos,pos-oldpos));
            oldpos = pos+test.len[1];
            test = REFind(regex,str,oldpos,1);
            pos = test.pos[1];
        }
        //Thanks to Thomas Muck
        if(len(str) gte oldpos) arrayappend(results,right(str,len(str)-oldpos + 1));
    
        if(len(str) lt oldpos) arrayappend(results,"");
    
        return results;
    }
    </cfscript>
    <cfdump var="#cookie#">
    <cfset x = ListToArray(favorites)>
    <cfdump var="#x#">
    <cfloop index="n" from="1" to="#arrayLen(x)#">
    <cfoutput>#x[n]#<br /></cfoutput>
    </cfloop>
    
    
    Code (markup):
    Please Show me a way how do i implement it
     
    gavy, Aug 1, 2009 IP