0

I am making a simple questionnaire for a client in Classic ASP.

The idea is that there will be 10 questions. The user registers and is being sent to the first question. When this is answered they move on to the 2nd question etc. Questions can be skipped and returned to at a later date, and each question can only be answered once.

I have a comma separated list in the database of each question a user has answered.

So, a user logs in and an array is created with the list of answered questions.

What would be the best way to loop through this list and go to the first unanswered question?

An example of the array of answered questions would look something like this "1,4,6" so this user would have answered questions number 1, 4 and 6. When a user logs in I'd like to direct them to the first unanswered question, in this case 2. Once the second question is answered the user would be redirected to the next unanswered question.

Any suggestions please?

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
Dog
  • 647
  • 1
  • 9
  • 28
  • 1
    What have you tried so far? Also, you store a comma-separated list in your database, instead of one value per record?? – LittleBobbyTables - Au Revoir Aug 21 '12 at 15:06
  • 1
    Saving id's as a csv in a table column is a bad practice, it would be better if you save the questions answered in another table using user id as a foreign key, then you could easily pull the first unanswered question from this table. – 03Usr Aug 21 '12 at 15:07
  • i don't usually use csv in my database, i just thought for this 'problem' it would be the easiest way to go... i have an array of the answered questions, i just want to find a good way to loop through them and go to first unanswered... i could change to using a recordset instead of an array of values, but i'm still missing a clean idea for finding 1st unanswered... i'm worried i'm having one of those 'stupid' days. – Dog Aug 21 '12 at 15:16
  • i have such an asp app but it presents all of the questions at once in a list with checkboxes, combo's, free form text etc and i store the results per user/session_id or something else in a textfile on the server, afterward a vbscript pulls everything together and presents a csv into Excel with the data. configuration of the questions is in an array, if you like i make an answer with some meaningful peaces, the asp and vbscript are too large to publish all – peter Aug 21 '12 at 15:43
  • thanks for the offer peter, but i'm not sure how relevant that would be... thanks anyway... i thought this was a reasonably simple question, but its obviously more complicated than i thought :)... so, i've decided to rework the page so the user has a list of the possible questions, with answered questions greyed out... hopefully this will prove to be easier :) – Dog Aug 22 '12 at 12:16

1 Answers1

0

@Dog, I think this offers the functionality you are looking for.

Tip: See this answer for information on downloading Microsoft's authoritative WSH reference as a Windows help file.

Option Explicit

Dim oQsm : Set oQsm = New QuestionStatusManager

With oQsm
    .NumberOfQuestions = 10
    .RestoreStatus("1,4,6")
    .MarkQuestionAnswered(2)
    WScript.Echo "Questions " & .ToString() & " have been answered."
    WScript.Echo "Next unanswered question is: " & .GetNextUnansweredQuestion()
End With

Set oQsm = Nothing

' ------------------------------------------------------------------------

Class QuestionStatusManager

Private m_nNumberOfQuestions
Private m_aQuestionList()

Sub Class_Initialize()
    m_nNumberOfQuestions = -1
End Sub

Sub Class_Terminate()
    Erase m_aQuestionList
End Sub

Public Property Let NumberOfQuestions(n)
    Dim bValid : bValid = False

    If IsNumeric(n) Then
        If n = CInt(n) Then
            bValid = True
        End If
    End If

    If Not bValid Then
        Err.Raise vbObjectError + 1, "", _
           "Value '" & n & "' is not an integer."
    End If

    m_nNumberOfQuestions = CInt(n)
    ReDim m_aQuestionList(n)
End Property

Public Property Get NumberOfQuestions()
    CheckState
    NumberOfQuestions = m_nNumberOfQuestions
End Property

Private Sub CheckState()
    If m_nNumberOfQuestions = -1 Then
        Err.Raise vbObjectError + 1, "", _
           "Property 'NumberOfQuestions' has not been set."
    End If
End Sub

Sub RestoreStatus(sAlreadyAnswered)
    CheckState

    Dim aAlreadyAnswered : aAlreadyAnswered = Split(sAlreadyAnswered, ",")
    Dim i

    For i = 0 To UBound(m_aQuestionList)
        m_aQuestionList(i) = False
    Next

    For i = 0 To UBound(aAlreadyAnswered)
        m_aQuestionList(CInt(aAlreadyAnswered(i))) = True
    Next
End Sub

Sub MarkQuestionAnswered(n)
    Dim sDesc

    CheckState

    On Error Resume Next
    m_aQuestionList(n) = True

    If Err Or n = 0 Then
        sDesc = Err.Description
        On Error GoTo 0
        Err.Raise vbObjectError + 1, "", _
           "Can't mark question number '" & n & "' as answered: " & sDesc
    End If
End Sub

Function GetNextUnansweredQuestion()
    CheckState

    Dim i

    For i = 1 To UBound(m_aQuestionList)
        If Not m_aQuestionList(i) Then
            GetNextUnansweredQuestion = i
            Exit Function
        End If
    Next

    GetNextUnansweredQuestion = -1
End Function

Function ToString()
    CheckState

    Dim sDelim : sDelim = ""
    Dim i

    ToString = ""
    For i = 1 To UBound(m_aQuestionList)
        If m_aQuestionList(i) Then
            ToString = ToString & sDelim & CStr(i)
            sDelim = ","
        End If
    Next
End Function

End Class
Community
  • 1
  • 1
DavidRR
  • 18,291
  • 25
  • 109
  • 191
  • thanks for the answer david... i've never used the WSH library before... it does look quite complicated for something i presumed would be fairly simple... as i said in the comments above i'm going to find another way of presenting the questions as this just seems too much trouble :) thanks again though for your answer – Dog Aug 22 '12 at 12:18
  • 1
    @Dog, one of the points I'm making here is that even VBScript and the classic Active Server Pages allow you to express yourself in an object-oriented fashion. Learning about object-oriented programming will serve you well as you move into more modern languages such as VB.NET, Java, C# and many more. – DavidRR Aug 22 '12 at 19:12