突出显示经典ASP中的特定记录

时间:2011-08-04 10:31:35

标签: arrays loops asp-classic record

在我的quiz page中,我有十个问题。当用户经历这些问题时,我将错误问题的ID存储在数组中。当用户点击“显示结果”链接时,我将显示所有10个问题及其答案。我想以粗体字母突出错误回答的问题。

这是我的代码:

Dim DB_CONN_STRING
DB_CONN_STRING = "DBQ=" & Server.MapPath("quiz.mdb") & ";"
DB_CONN_STRING = DB_CONN_STRING & "Driver={Microsoft Access Driver (*.mdb)};"
DB_CONN_STRING = DB_CONN_STRING & "DriverId=25;FIL=MS Access;"

Const QUIZ_ID = 1

Dim panswer 

panswer=split((Request.QueryString("marked")),",")

''# collecting wrongly answered question id in panswer
Dim cnnQuiz, rsQuiz, I
Set cnnQuiz = Server.CreateObject("ADODB.Connection")
cnnQuiz.Open DB_CONN_STRING
Set rsQuiz = Server.CreateObject("ADODB.Recordset")
rsQuiz.Open "SELECT * FROM questions WHERE quiz_id=" & QUIZ_ID & " ORDER BY question_number;", cnnQuiz, 3, 1
Do While Not rsQuiz.EOF
    For  I = LBound(panswer) to UBound(panswer)
        if (CStr(rsQuiz.Fields("question_number").Value)) = CStr(panswer(i)) then
            ''# testing whether this question is wrongly answered and if its true then making bold letters.
            response.Write("<b>"&(rsQuiz.Fields("question_number").Value)&")</b>") 
            response.Write("<b>"&(rsQuiz.Fields("question_text").Value)&"</b><br>")
            response.Write("<b>"&(rsQuiz.Fields("correct_answer").Value)&"</b><br>")
        end if
    Next 
    '' # else displaying all 10 questions
    response.Write((rsQuiz.Fields("question_number").Value)&") ") 
    response.Write((rsQuiz.Fields("question_text").Value)&"<br>")
    response.Write((rsQuiz.Fields("correct_answer").Value)&"<br>")
    rsQuiz.MoveNext
Loop
rsQuiz.Close
Set rsQuiz = Nothing
cnnQuiz.Close
Set cnnQuiz = Nothing

问题:此代码显示所有10条记录,但只有数组中的第一项突出显示,并且重复两次。这是一个现有的网站,我必须保持经典的ASP。

1 个答案:

答案 0 :(得分:1)

在我看来,你应该使用循环来确定make是否粗体但不包括循环中的输出。然后使用简单的if发送一个输出或另一个。

Dim DB_CONN_STRING
DB_CONN_STRING = "DBQ=" & Server.MapPath("quiz.mdb") & ";"
DB_CONN_STRING = DB_CONN_STRING & "Driver={Microsoft Access Driver (*.mdb)};"
DB_CONN_STRING = DB_CONN_STRING & "DriverId=25;FIL=MS Access;"

Const QUIZ_ID = 1

Dim panswer 

panswer=split((Request.QueryString("marked")),",")

''# collecting wrongly answered question id in panswer
Dim cnnQuiz, rsQuiz, I
Set cnnQuiz = Server.CreateObject("ADODB.Connection")
cnnQuiz.Open DB_CONN_STRING
Set rsQuiz = Server.CreateObject("ADODB.Recordset")
rsQuiz.Open "SELECT * FROM questions WHERE quiz_id=" & QUIZ_ID & " ORDER BY question_number;", cnnQuiz, 3, 1

Dim bMakeBold
Do While Not rsQuiz.EOF

    bMakeBold = False;
    For  I = LBound(panswer) to UBound(panswer)
        ''# testing whether this question is wrongly answered and if its true then making bold letters.
        If (CStr(rsQuiz.Fields("question_number").Value)) = CStr(panswer(i)) Then
            bMakeBold = True
            Exit For
        End If
    Next

    if bMakeBold then 
        response.Write("<b>"&(rsQuiz.Fields("question_number").Value)&")</b>") 
        response.Write("<b>"&(rsQuiz.Fields("question_text").Value)&"</b><br>")
        response.Write("<b>"&(rsQuiz.Fields("correct_answer").Value)&"</b><br>")
    else
        response.Write((rsQuiz.Fields("question_number").Value)&") ") 
        response.Write((rsQuiz.Fields("question_text").Value)&"<br>")
        response.Write((rsQuiz.Fields("correct_answer").Value)&"<br>")
    end if

    rsQuiz.MoveNext
Loop
rsQuiz.Close
Set rsQuiz = Nothing
cnnQuiz.Close
Set cnnQuiz = Nothing