Instr problems  | Familiar Sight | | Join Date: Nov 2007
Posts: 182
| |
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 - 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
|  | Expert | | Join Date: Oct 2008 Location: Bristol, United Kingdom
Posts: 143
| | | 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: -
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 -
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
|  | Familiar Sight | | Join Date: Nov 2007
Posts: 182
| | | re: Instr problems
I understand this but it is returning an error with the sql statement that uses the session variable?? - "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
|  | Expert | | Join Date: Oct 2008 Location: Bristol, United Kingdom
Posts: 143
| | | 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. -
<%
-
-
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
|  | Familiar Sight | | Join Date: Nov 2007
Posts: 182
| | | 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
|  | Expert | | Join Date: Oct 2008 Location: Bristol, United Kingdom
Posts: 143
| | | re: Instr problems
That now produces 0,1634,2089,1638,1633 using the modified loop in my prior post.
Can you test that now?
|  | Familiar Sight | | Join Date: Nov 2007
Posts: 182
| | | re: Instr problems
I now have - <%
-
-
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?
|  | Familiar Sight | | Join Date: Nov 2007
Posts: 182
| | | re: Instr problems
hi it now works i removed the -1
its now - <%
-
-
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")
-
-
%>
|  | Familiar Sight | | Join Date: Nov 2007
Posts: 182
| | | 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 - Response.write("<form action=""boys2.asp?id=" & yaketyRecordset("idnumber") & "&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
|  | Familiar Sight | | Join Date: Nov 2007
Posts: 182
| | | 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!!!!!
|  | Moderator | | Join Date: Jan 2007 Location: logan, utah
Posts: 2,690
| | | re: Instr problems Quote:
Originally Posted by colinod 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!!!!! |  | Familiar Sight | | Join Date: Nov 2007
Posts: 182
| | | 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 - <%
-
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
-
%>
|  | Moderator | | Join Date: Jan 2007 Location: logan, utah
Posts: 2,690
| | | 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. - <%
-
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
-
%>
|  | Familiar Sight | | Join Date: Nov 2007
Posts: 182
| | | re: Instr problems
changed it to straight HTML and its works fine thanks for your help
|  | Similar ASP / Active Server Pages bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,358 network members.
|