SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: Instr Function

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    Egypt
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Instr Function

    I have a multiple selection list box that lists some items, and I have 2 modules:
    1 - Insertion module: it is the time when user select some items and inserts them in db.
    2 - Udate Module: It is time when the user want to re-select the items thus I have a page that displays the list box with the items that were previously selected by the user CHECKED/SELECTED/Highlighted in the list box.

    suppose the user selects items 10, 11 and 12 at the insertion time, now at the update time the pre-selected items that return from db comes in a string as "10, 11, 12"

    I use the Instr function to search for a list item value in the returned string "10, 11, 12" so if an item value is found it is selected.

    The problem is that if items 10 and 11 and 12 were selected by the user in the insertion module, thus items 10 and 11 and 12 should be only selected in the list box but in fact items 10 and 11 and 12 AND Items 1 and 2 are also selected which is not correct.
    How can I fix this problem.


    Here is the list box
    <Select Multiple Name = "Sel_Lessons">
    <Option Value = "XX" >-- Select --
    <Option Value = "1" <%If Not InStr(LessonIds, Cint("1")) = 0 Then Response.write "Selected"%>> Art and Desgin
    <Option Value = "2" <%If Not InStr(LessonIds, "2") = 0 Then Response.write "Selected"%>> Citizenship
    <Option Value = "3" <%If Not InStr(LessonIds, "3") = 0 Then Response.write "Selected"%>> Desgin and Technology
    <Option Value = "4" <%If Not InStr(LessonIds, "4") = 0 Then Response.write "Selected"%>> English
    <Option Value = "5" <%If Not InStr(LessonIds, "5") = 0 Then Response.write "Selected"%>> Geography
    <Option Value = "6" <%If Not InStr(LessonIds, "6") = 0 Then Response.write "Selected"%>> History
    <Option Value = "7" <%If Not InStr(LessonIds, "7") = 0 Then Response.write "Selected"%>> ICT
    <Option Value = "8" <%If Not InStr(LessonIds, "8") = 0 Then Response.write "Selected"%>> Mathematics
    <Option Value = "9" <%If Not InStr(LessonIds, "9") = 0 Then Response.write "Selected"%>> Music
    <Option Value = "10" <%If Not InStr(LessonIds, "10") = 0 Then Response.write "Selected"%>> PE
    <Option Value = "11" <%If Not InStr(LessonIds, "11") = 0 Then Response.write "Selected"%>> PSHE
    <Option Value = "12" <%If Not InStr(LessonIds, "12") = 0 Then Response.write "Selected"%>> Religious Education
    <Option Value = "13" <%If Not InStr(LessonIds, "13") = 0 Then Response.write "Selected"%>> Science
    </Select>


    Thanks a lot

  2. #2
    SitePoint Evangelist
    Join Date
    Nov 2002
    Posts
    423
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use the VBScript Split function to split your list by comma and then loop through the result array to compare the array values with your var:

    Code:
     
    arrValues = Split("10, 11, 12", ",")
    For i = 0 To UBound(arrValues)
    	If arrValues(i) = YourVar Then
    	 sSelected = " selected"
    	End If
    Next

  3. #3
    Learning...
    Join Date
    Jan 2003
    Posts
    781
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can do as asp-hosting.ca has shown or make all of your list box values to be same size, 1 will become 01, 02, 03, 04.....09,10......

    OR for values from 1 - 9 also check the length of the value too

    <%If Not InStr(LessonIds, Cint("1")) = 0 and length(CStr(LessonIds)) = 1 Then Response.write "Selected"%>>

    plz double check the syntex for length, in javascript it is str.length. I dont know in VBscript.
    The beauty of life is not dependent on how happy you are,
    but on how happy others can be because of you...


Bookmarks

Posting Permissions

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