Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2011
    Location
    Spain [near Alicante]
    Posts
    10
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Cookie array problem

    Going bald with this one

    I have a cookie that has a series of numbers separated by commas. I can read this array, but I need to check if a particular number exists in this array in order to write in a line.

    Example:
    My array = "62,430,26,92" [is read from a cookie]

    If my item id is say 62, I want to add the extra line to item 62.

    Currently it seems to be adding the line to all records, so I need some sort of routine to find the item id in the array, and if not, ignore the line.

    I was given this piece of code, but it is not clear what it is doing, and is not working.

    Code:
    <%
    pds=Request.Cookies("pids")
    propid = SProps("PropID")
    
    Dim iLoop
    
    For iLoop = LBound(pds) to UBound(pds)
      If CStr(pds(iLoop)) = CStr(propid) then
    %>
    Here I write in my line of text if the id numbers match
    <%
    End If
    Next
    %>
    Any help would be apprecated

  • #2
    New Coder
    Join Date
    Jul 2011
    Location
    Spain [near Alicante]
    Posts
    10
    Thanks
    1
    Thanked 2 Times in 2 Posts
    I have found a temporary work-around using a Db lookup

    Code:
    CSQL = "SELECT * FROM property WHERE PropID IN(" & pids & ") AND PropID="&cpropid
    then using a .EOF statement to decide to write the line or not.

    I would however like to streamline this a little using Javascript/VBScript, as 500 Db lookups per page is going to have an impact on the server

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,596
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Try this:
    Code:
    cookieList = "62,430,26,92" ' that's a *LIST*, it is *NOT* an array!!!  get value from cookie
    
    cookieListFix = "," & cookieList & "," ' tack a comma onto front and end of the list
    
    lookFor = 62 ' wherever you get this from
    
    If InStr( cookieListFix, "," & lookFor & "," ) > 0 Then
       ... found ...
    Else
       ... not found ...
    End If
    Reason we tack on the commas: If you simply did
    Code:
        If InStr( cookieList, 6 ) > 0 Then ...
    You would get a FALSE MATCH for 6 on both "62" and "26".

    By putting the commas on the front and back of both the list *AND* the value you are looking for, you don't get any false matches.

    The other way:
    Code:
    cookieList = "62,430,26,92" ' that's a *LIST*, it is *NOT* an array!!!  get value from cookie
    
    cookieArray = Split( cookieList, "," ) ' *now* you have a true array
    lookFor = 26 ' example
    For c = 0 To UBound( cookieArray )
        If CLNG(cookieArray(c)) = lookFor Then
            found = True
            Exit For
       End If
    Next
    If found Then 
        ...
    Else
       ...
    End If
    And that works, but it will be somewhat slower than the INSTR solution. And if you are really going to do this 500 times per page...

  • Users who have thanked Old Pedant for this post:

    NomadicT (07-23-2011)

  • #4
    New Coder
    Join Date
    Jul 2011
    Location
    Spain [near Alicante]
    Posts
    10
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Many thanks for your help with this one. I will use the first example as it will run far more quickly, and because of its simplicity to understand and manipulate.

    I sort of knew it wasn't a true array, but didn't know what else to call it!

    I will try to paginate rather than have the odd occasion when it needs to be called 500 [actually more like 700] times. Normally it will only run 20-30 times.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •