Connecting Tech Pros Worldwide Help | Site Map

Instr problems

colinod's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 182
#1: May 7 '09
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

Expand|Select|Wrap|Line Numbers
  1. dim firstHalf, secondHalf  
  2. firstHalf = left(session("recordsInCart"), instr(request.form("recordNum")))  
  3. secondHalf = right(session("recordsInCart"), len(session("recordsInCart")) - instr(request.form("recordNum")) - len (request.form("recordNum"))) 
  4. 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's Avatar
Expert
 
Join Date: Oct 2008
Location: Bristol, United Kingdom
Posts: 143
#2: May 7 '09

re: Instr problems


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

So:

Expand|Select|Wrap|Line Numbers
  1. left(session("recordsInCart"), instr(session("recordsInCart"),request.form("recordNum")))
  2.  
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

Expand|Select|Wrap|Line Numbers
  1. If Not Request.Form("RecordNum") ="" Then
  2. CartItems = split(session("recordsInCart"),",")
  3. For Each Item in CartItems
  4. If Not Item = Request.Form("RecordNum") Then newitems = newitems & Item & ","
  5. Next
  6. session("recordsInCart") = newitems
  7. End If
  8.  
Gaz
colinod's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 182
#3: May 7 '09

re: Instr problems


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

Expand|Select|Wrap|Line Numbers
  1. "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
GazMathias's Avatar
Expert
 
Join Date: Oct 2008
Location: Bristol, United Kingdom
Posts: 143
#4: May 7 '09

re: Instr problems


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.

Expand|Select|Wrap|Line Numbers
  1. <%
  2.  
  3. mytest= "1,2,3,4,5,6,7,8,9,10,"
  4.  
  5. remove = "4"
  6.  
  7. If Not remove ="" Then
  8. CartItems = split(mytest,",")
  9. For Each Item in CartItems
  10. If Not Item = remove Then
  11. If IsNnumeric(Item) then newitems = newitems & Item & ","
  12. End If
  13. Next
  14. mytest = left(newitems, len(newitems)-1) 'remove trailing comma from each loop iteration
  15. End If
  16.  
  17. Response.Write mytest
  18.  
  19. %>
  20.  
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
colinod's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 182
#5: May 7 '09

re: Instr problems


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

0,1634,2089,1638,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,1633,,)'.

the can be seen if you follow one of the artists on the following link
www.yaketyyakallmouth.com/pages/boys.asp?id=all

not sure why the 0 is at the front either but thats always been there
GazMathias's Avatar
Expert
 
Join Date: Oct 2008
Location: Bristol, United Kingdom
Posts: 143
#6: May 7 '09

re: Instr problems


That now produces 0,1634,2089,1638,1633 using the modified loop in my prior post.

Can you test that now?
colinod's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 182
#7: May 7 '09

re: Instr problems


I now have

Expand|Select|Wrap|Line Numbers
  1. <% 
  2.  
  3. If Not request.form("recordNum") ="" Then 
  4. CartItems = split(session("recordsInCart") ,",") 
  5. For Each Item in CartItems 
  6. If Not Item = request.form("recordNum") Then 
  7. If IsNumeric(Item) then newitems = newitems & Item & "," 
  8. End If 
  9. Next 
  10. session("recordsInCart") = left(newitems, len(newitems)-1) 'remove trailing comma from each loop iteration 
  11. End If 
  12.  
  13. Response.Write session("recordsInCart") 
  14.  
  15. %> 
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?
colinod's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 182
#8: May 7 '09

re: Instr problems


hi it now works i removed the -1

its now

Expand|Select|Wrap|Line Numbers
  1. <%  
  2.  
  3. If Not request.form("recordNum") ="" Then  
  4. CartItems = split(session("recordsInCart") ,",")  
  5. For Each Item in CartItems  
  6. If Not Item = request.form("recordNum") Then  
  7. If IsNumeric(Item) then newitems = newitems & Item & ","  
  8. End If  
  9. Next  
  10. session("recordsInCart") = left(newitems, len(newitems)) 'remove trailing comma from each loop iteration  
  11. End If  
  12.  
  13. Response.Write session("recordsInCart")  
  14.  
  15. %>  
colinod's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 182
#9: May 7 '09

re: Instr problems


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.form("recordNum")) = 0 then
session("recordsInCart") = session("recordsInCart") + request.form("recordNum") &","
else
'do nothing
end if
%>

and the form that contains the button is

Expand|Select|Wrap|Line Numbers
  1. Response.write("<form action=""boys2.asp?id=" & yaketyRecordset("idnumber") & "&amp;voicetype=" &voiceid &""" method=""post"">" & vbNewline)%>
  2.                                       <TD>
  3.                                     <input type="hidden" name="recordNum" value="<%=dlRecordset("idnumbermp3")%>">
  4.                                     </TD>
  5.                                       <TD><INPUT name="submit" type="image" src="../images/makeup/shortlistpink.gif" alt="Add to shortlist" align="bottom" border="0"></TD>
  6.                                     </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
colinod's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 182
#10: May 8 '09

re: Instr problems


hi

Using my session("recordsInCart") 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!!!!!
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#11: May 11 '09

re: Instr problems


Quote:

Originally Posted by colinod View Post

hi

Using my session("recordsInCart") 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!!!!!

Expand|Select|Wrap|Line Numbers
  1. if instr(...) > 0 then
colinod's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 182
#12: May 12 '09

re: Instr problems


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

Expand|Select|Wrap|Line Numbers
  1. <%
  2. if InStr(session("recordsInCart"), ","&dlRecordset("idnumbermp3")) > 0 then 
  3. Response.Write("<INPUT name=""submit"" type=""image"" src=""../images/makeup/shortlistpink.gif"" alt=""Add to shortlist"" align=""bottom"" border=""0"">") & vbCRLF
  4. else 
  5. Response.Write("<INPUT name=""submit"" type=""image"" src=""../images/makeup/shortlistyellow.gif"" alt=""Add to shortlist"" align=""bottom"" border=""0"">") & vbCRLF
  6. end if 
  7. %>
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#13: May 12 '09

re: Instr problems


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.

Expand|Select|Wrap|Line Numbers
  1. <%
  2. if InStr(session("recordsInCart"), ","&dlRecordset("idnumbermp3")) > 0 then 
  3.  %>
  4. <INPUT name="submit" type="image" src="../images/makeup/shortlistpink.gif" alt="Add to shortlist" align="bottom" border="0">
  5. <%
  6. else %>
  7. <INPUT name="submit" type="image" src="../images/makeup/shortlistyellow.gif" alt="Add to shortlist" align="bottom" border="0">
  8. <%
  9. end if 
  10. %>
colinod's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 182
#14: May 15 '09

re: Instr problems


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


Similar ASP / Active Server Pages bytes