Highlighting specific records in classic ASP
In my quiz page, I have ten questions. When the user goes through these questions, I store the IDs of wrong questions in a array. When the user clicks on the "show result" link, I'm going to display all 10 questions with their answers. I want to highlight wrongly answered question with bold letters.
Here's my code:
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
Problem: this code displays 开发者_运维百科all 10 records, but only the first item in the array gets highlighted, and it repeats twice. This is an existing website and I have to keep to classic ASP.
Looks to me like you should be using the loop to determine whether the make bold or not but not include output in the loop. Then use a simple if to send one output or the other.
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
精彩评论