Instr problems

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • colinod
    Contributor
    • Nov 2007
    • 347

    Instr problems

    I have a page that uses a session variable that stores items like a shopping cart, all this works fine but i am trying to code a button that removes an item, i have a page that i have set up for the remove function and have tried the following code

    Code:
    dim firstHalf, secondHalf  
    firstHalf = left(session("recordsInCart"), instr(request.form("recordNum")))  
    secondHalf = right(session("recordsInCart"), len(session("recordsInCart")) - instr(request.form("recordNum")) - len (request.form("recordNum"))) 
    session("recordsInCart")=firstHalf&secondHalf
    this returns an error like this

    this is returning an error

    Microsoft VBScript runtime error '800a01c2'

    Wrong number of arguments or invalid property assignment: 'instr'

    not sure where to go from here as i know nothing about this

    all i want to do is click on a button on the page that will send the variable recordNum from a form on the page back to the page and remove the numbers contained in this variable

    e.g.
    session variable = 1,2,3,4,5,6,7,8 ,9

    recordNum = 4

    click the button and remove 4 so that the session variable = 1,2,3,5,6,7,8,9

    hope i have explained this properly
  • GazMathias
    Recognized Expert New Member
    • Oct 2008
    • 228

    #2
    As a minimum amount of arguments, you need to provide instr() with the string to search and the string to find.

    So:

    Code:
    left(session("recordsInCart"), instr(session("recordsInCart"),request.form("recordNum")))
    Looking at it though I think replace() or split() may be better options.

    For example using split you could add each recordNum to an array then loop through it adding each one back to your session variable except the one you don't want.

    For example

    Code:
    If Not Request.Form("RecordNum") ="" Then
    CartItems = split(session("recordsInCart"),",")
    For Each Item in CartItems
    If Not Item = Request.Form("RecordNum") Then newitems = newitems & Item & ","
    Next
    session("recordsInCart") = newitems
    End If
    Gaz

    Comment

    • colinod
      Contributor
      • Nov 2007
      • 347

      #3
      I understand this but it is returning an error with the sql statement that uses the session variable??

      Code:
      "SELECT * FROM mp3 INNER JOIN celebs ON mp3.celebid = celebs.idnumber WHERE idnumbermp3 in (" & session("recordsInCart") & ") ORDER BY celebs.surname"
      i have added a response.write on the last line and it is removing the item numbers but leaving the commas? how can i get rid of them as they must be causing the problem

      any ideas it worked with the old code

      Comment

      • GazMathias
        Recognized Expert New Member
        • Oct 2008
        • 228

        #4
        Can you post here the session variable before and after the items have been removed so I can take a look see?

        I probably just need to modify the loop slightly (I didn't test it!)

        Never mind that.

        I copied the code from here and modified it.

        Code:
        <%
        
        mytest= "1,2,3,4,5,6,7,8,9,10,"
        
        remove = "4"
        
        If Not remove ="" Then
        CartItems = split(mytest,",")
        For Each Item in CartItems
        If Not Item = remove Then
        If IsNnumeric(Item) then newitems = newitems & Item & ","
        End If
        Next
        mytest = left(newitems, len(newitems)-1) 'remove trailing comma from each loop iteration
        End If
        
        Response.Write mytest
        
        %>
        This prduces 1,2,3,5,6,7,8,9 ,10 either with or without a trailing comma in the initial variable.

        Just change my variables to yours.

        Gaz

        Comment

        • colinod
          Contributor
          • Nov 2007
          • 347

          #5
          This is the session variable from the page previous to the delete page

          0,1634,2089,163 8,1633,

          this is the error i get when i try and delete an item on the delete page

          [Microsoft][ODBC Microsoft Access Driver] Syntax error (missing operator) in query expression 'idnumbermp3 in (0,1634,2089,16 33,,)'.

          the can be seen if you follow one of the artists on the following link


          not sure why the 0 is at the front either but thats always been there

          Comment

          • GazMathias
            Recognized Expert New Member
            • Oct 2008
            • 228

            #6
            That now produces 0,1634,2089,163 8,1633 using the modified loop in my prior post.

            Can you test that now?

            Comment

            • colinod
              Contributor
              • Nov 2007
              • 347

              #7
              I now have

              Code:
              <% 
              
              If Not request.form("recordNum") ="" Then 
              CartItems = split(session("recordsInCart") ,",") 
              For Each Item in CartItems 
              If Not Item = request.form("recordNum") Then 
              If IsNumeric(Item) then newitems = newitems & Item & "," 
              End If 
              Next 
              session("recordsInCart") = left(newitems, len(newitems)-1) 'remove trailing comma from each loop iteration 
              End If 
                
              Response.Write session("recordsInCart") 
                
              %>
              this works fine, but if i go back to another artist after deleting something it does not have a comma in the middle of the old number and the new one?

              Comment

              • colinod
                Contributor
                • Nov 2007
                • 347

                #8
                hi it now works i removed the -1

                its now

                Code:
                <%  
                  
                If Not request.form("recordNum") ="" Then  
                CartItems = split(session("recordsInCart") ,",")  
                For Each Item in CartItems  
                If Not Item = request.form("recordNum") Then  
                If IsNumeric(Item) then newitems = newitems & Item & ","  
                End If  
                Next  
                session("recordsInCart") = left(newitems, len(newitems)) 'remove trailing comma from each loop iteration  
                End If  
                  
                Response.Write session("recordsInCart")  
                  
                %>

                Comment

                • colinod
                  Contributor
                  • Nov 2007
                  • 347

                  #9
                  just wondering if anyone could help with something else, i want to change the image of the button used to add something to the cart if it exists in the cart already

                  the code used to add to the cart is the following

                  <%

                  if InStr(session(" recordsInCart") , ","&request.for m("recordNum" )) = 0 then
                  session("record sInCart") = session("record sInCart") + request.form("r ecordNum") &","
                  else
                  'do nothing
                  end if
                  %>

                  and the form that contains the button is

                  Code:
                  Response.write("<form action=""boys2.asp?id=" & yaketyRecordset("idnumber") & "&amp;voicetype=" &voiceid &""" method=""post"">" & vbNewline)%>
                                                        <TD>
                                                      <input type="hidden" name="recordNum" value="<%=dlRecordset("idnumbermp3")%>">
                                                      </TD>
                                                        <TD><INPUT name="submit" type="image" src="../images/makeup/shortlistpink.gif" alt="Add to shortlist" align="bottom" border="0"></TD>
                                                      </form>
                  i know this must be really simple just to ask if the id number is in the session variable and then say the image is......

                  just not that good with asp

                  Comment

                  • colinod
                    Contributor
                    • Nov 2007
                    • 347

                    #10
                    hi

                    Using my session("record sInCart") could i find if my number stored in recordNum is in it and then change a picture depending on it, its so i can have the button that adds to the cart show a different picture if its already in the session variable

                    sorry ive already asked you this!!!!!

                    Comment

                    • jhardman
                      Recognized Expert Specialist
                      • Jan 2007
                      • 3405

                      #11
                      Originally posted by colinod
                      hi

                      Using my session("record sInCart") could i find if my number stored in recordNum is in it and then change a picture depending on it, its so i can have the button that adds to the cart show a different picture if its already in the session variable

                      sorry ive already asked you this!!!!!
                      Code:
                      if instr(...) > 0 then

                      Comment

                      • colinod
                        Contributor
                        • Nov 2007
                        • 347

                        #12
                        Hi
                        I think i understand what you mean, i have tried to code it so that depending on the Instr result it writes a different line of code for the submit button, only problem is that no button appears, im sure ive done something wrong in my code

                        Code:
                        <%
                        if InStr(session("recordsInCart"), ","&dlRecordset("idnumbermp3")) > 0 then 
                        Response.Write("<INPUT name=""submit"" type=""image"" src=""../images/makeup/shortlistpink.gif"" alt=""Add to shortlist"" align=""bottom"" border=""0"">") & vbCRLF
                        else 
                        Response.Write("<INPUT name=""submit"" type=""image"" src=""../images/makeup/shortlistyellow.gif"" alt=""Add to shortlist"" align=""bottom"" border=""0"">") & vbCRLF
                        end if 
                        %>

                        Comment

                        • jhardman
                          Recognized Expert Specialist
                          • Jan 2007
                          • 3405

                          #13
                          For troubleshooting purposes and readability, take it out of the response.write. Usually this type of error is just a result of too many quote marks in the response.write. Remember that HTML is much more straight-forward, so if you can write it all int plain HTML do it.

                          Code:
                          <%
                          if InStr(session("recordsInCart"), ","&dlRecordset("idnumbermp3")) > 0 then 
                           %>
                          <INPUT name="submit" type="image" src="../images/makeup/shortlistpink.gif" alt="Add to shortlist" align="bottom" border="0">
                          <%
                          else %>
                          <INPUT name="submit" type="image" src="../images/makeup/shortlistyellow.gif" alt="Add to shortlist" align="bottom" border="0">
                          <%
                          end if 
                          %>

                          Comment

                          • colinod
                            Contributor
                            • Nov 2007
                            • 347

                            #14
                            changed it to straight HTML and its works fine thanks for your help

                            Comment

                            Working...