By Ryan S.
If you have read this message board post, you will see that someone asked how to create a multi-question quiz. Here is my response, in tutorial form.
First, it depends if you will be using this quiz format over - eg, a weekly quiz. I'll assume that you will. If you haven't noticed from my other posts, I'm extremely pro-database. Let's create a simple database called myDatabase.mdb (assuming its MS Access). Then create a table called Questions. Now, create these records. Also, I'll assume that its like a book type quiz. If the user misses the question, it shows them where in the book they can find the information.
| QuestionID | AutoNumber |
| Question | Text 255 |
| AnswerA | Text 255 |
| AnswerB | Text 255 |
| AnswerC | Text 255 |
| AnswerD | Text 255 |
| CorrectAns | Number 1 (the reason it's number is because you say which is correct. AK - 1 = A, 2 = B, etc) |
| Reference | Text255 |
Now, fill in your questions (probably using a form in Access, which is easiest). Now for the ASP
I'm assuming that you have put in your <HTML>, <HEAD>, and <BODY>. This goes directly below <BODY>
<%
This first part puts the values from the forms (if any) into variables. Saves
on typing. Also, include the PoorManIsNull routine between these two areas of
code
CurQ = Request.Form("CurQ")
Answ = Request.Form("Answ")
correct=Request.Form("Correct")
wrong=Request.Form("Wrong")
If PoorMansIsNull(CurQ) Then
If they don't have an answer, then they are coming from the response page
(that tells them if they got their previous question right) If this is the
case, move them to the next question and display it.
'If they are just loading the page for
'the first time,
'set the current question number
CurQ = 1
correct = 0
wrong = 0
End If
If PoorMansIsNUll(Answ) Then
CurQ = CurQ + 1
If they have completed the test, let them know, show them their score. and be
done with it.
If CurQ > (Your maximum number of questions) Then %>
Congratulations. You have completed this test. You missed <%=wrong%>
questions,
but got <%=correct%> questions right. That is equivilent to a
<%=(correct/(max#ofQs)%>%.
Thank you for doing the test.
<%
This code opens the database and moves the record to the current question. If
you didn't know that, then you should take a web database review either on
4GuysFromRolla.com or
http://www.activeserverpages.com/learn
End If
set conntemp = server.createobject("adoDB.Connection")
myDSN = '(your DSN info goes here)
conntemp.Open myDSN
mySQL = "SELECT * FROM QUESTIONS WHERE QuestionID=" & CurQ
set rsTemp= conntemp.Execute(mySQL)
%>
<h2>Question Number <%=rsTemp("QuestionID")%> </h2>
Display their question.
<form method=POST action="myASP.ASP">
<input type=hidden name=CurQ value=<%=CurQ%>>
Your question is <%=rsTemp("Question")%>
I'm not sure about this code below. Essentially, you want to create a drop
down box where the first option is <%=rsTemp("AnswerA")%> and value=1,
and second is <%=rsTemp("AnswerB")%> and value=2. I'm not the best on forms,
so I tried:
Answer: <select name="AnsW">
That is the code for the drop down box. I realize that you may want to use
radioboxes in some cases. If this is the case, you would need to add a handler
for AnsW, where it is set to something like this:
<option value=1><%=rsTemp("AnswerA")</option>
<option value=2><%=rsTemp("AnswerB")</option>
<option value=3><%=rsTemp("AnswerC")</option>
<option value=4><%=rsTemp("AnswerD")</option>
</select>
If Request.Form("Radio1") Then ansW = 1
That is just a rough idea.
If Request.Form("Radio2") Then ansW = 2
If Request.Form("Radio3") Then ansW = 3
If Request.Form("Radio4") Then ansW = 4
<input type=hidden value="<%=correct%>">
This code just closes off the form:
<input type=hidden value="<%=wrong%>">
<input type=reset value="Clear the Form"><input type=submit value="OK!">
</form>
<%
If
Else
set conntemp = server.createobject("adoDB.Connection")
myDSN = '(your DSN info goes here)
conntemp.Open myDSN
mySQL = "SELECT * FROM QUESTIONS WHERE QuestionID=" & CurQ
set rsTemp= conntemp.Execute(mySQL)
AnsW isn't empty, then it must contain something, using logic. Therefore,
the logical step would be to compare this Answer with the actual Answer
<% Else
If AnsW = rsTemp("CorrectAns") Then
This closes out writing the responses, but we still aren't done yet. we now
add a button for the advancement of the questions. This appears on the
congratulations
and the sorry page because we already closed the If/Then checking whether they
were right.
'They got it right
%>
<p>Congratulations. You got it right. Whee</p>
<% correct = correct + 1 %>
'They missed the question %>
<p>I'm sorry, you missed the question. You can review by reading: </p>
<p><%=rsTemp("reference")</p>
<% wrong = wrong + 1 %>
<% End If %>
<form method=POST action="myASP.ASP">
Wow. That is it. All of the code is right there. Here, let me paste EVERY single
bit of the code for you as well. (Click here to view just the code.)
<input type="hidden" name=curQ value="<%=curQ%>">
<input type="hidden" name=correct value="<%=correct%>">
<input type="hidden" name=wrong value="<%=wrong%>">
<input type="submit" value="Next Question"%>
</form>
<% End If %>
CurQ = Request.Form("CurQ")
'Poor Man's IsNull Code goes here
If PoorMansIsNull(CurQ) Then
If PoorMansIsNUll(Answ) Then
<%
<h2>Question Number <%=rsTemp("QuestionID")%> </h2>
<form method=POST action="myASP.ASP">
<input type=hidden name=CurQ value=<%=CurQ%>>
<% Else %>
<form method=POST action="myASP.ASP">
<% End If %>
<%
Answ = Request.Form("Answ")
correct=Request.Form("Correct")
wrong=Request.Form("Wrong")
CurQ = 1
correct = 0
wrong = 0
End If
CurQ = CurQ + 1
If CurQ > (Your maximum number of questions) Then
%>
<p>Congratulations. You have completed this test. You missed <%=wrong%>
questions,
but got <%=correct%> questions right. That is equivilent to a
<%=(correct/(max#ofQs)%>%.
Thank you for doing the test.
<% End If %>
set conntemp = server.createobject("adoDB.Connection")
myDSN = '(your DSN info goes here)
conntemp.Open myDSN
mySQL = "SELECT * FROM QUESTIONS WHERE QuestionID=" & CurQ
set rsTemp= conntemp.Execute(mySQL)
%>
Your question is <%=rsTemp("Question")%><br>
Answer:
<select name="AnsW">
<option value=1><%=rsTemp("AnswerA")</option>
<option value=2><%=rsTemp("AnswerB")</option>
<option value=3><%=rsTemp("AnswerC")</option>
<option value=4><%=rsTemp("AnswerD")</option>
</select>
<input type=correct value="<%=correct%>"><input type=wrong value="<%=wrong%>">
<input type=reset value="Clear the Form"><input type=submit value="OK!">
</form>
<%
set conntemp = server.createobject("adoDB.Connection")
myDSN = '(your DSN info goes here)
conntemp.Open myDSN
mySQL = "SELECT * FROM QUESTIONS WHERE QuestionID=" & CurQ
set rsTemp= conntemp.Execute(mySQL)
If AnsW = rsTemp("CorrectAns") Then
%>
<p>Congratulations. You got it right. Whee</p>
<% Else %>
<% correct = correct + 1 %>
<p>I'm sorry, you missed the question. You can review by
reading: </p>
<p><%=rsTemp("reference")</p>
<% End If %>
<% wrong = wrong + 1 %>
<input type="hidden" name=curQ value="<%=curQ%>">
<input type="hidden" name=correct value="<%=correct%>">
<input type="hidden" name=wrong value="<%=wrong%>">
<input type="submit" value="Next Question"%>
</form>
Happy Programming!
Attachments:
This article was written by
Ryan S.
Ryan has been a computer programmer
in the loosest sense since the age of 8. He has been working with ASP
since the age of 13, when it first came out (that he knows of), and is
somewhat advanced at it.




