Hi there!
The following piece of code is based on a quiz.
It currently takes 'all' the records (questions and 4 corresponding answer choices)
from the databaseand displays them on a web page.
The database is made up of 6 fields, 1 question, 4 answer and 1 correctID field that
holds the number that corresponds to the correct answer position in each case.
When the quiz is completed the SUB Mark() function calculates the score and displays questions,
correct answers and the score on another page.
What I wanted to know was how I could randomize the quiz.
i.e Retrieve 10 random questions from a database table of 30 thirty questions
and display them and their answers on a page?? (The file is called quiz.asp)
Any help and suggestions would be greatrly appreciated!!
Thanks,
Portmoon.
<%
' On Error Resume Next
Set Conn = Server.CreateObject("ADODB.Connection"
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("myDatabase.mdb"
SQL = "SELECT * Quiz, conn, 3"
Set rs = conn.Execute(SQL)
IF Request.Form.Count = 0 THEN
CALL List
ELSE CALL Mark
END IF
rs.close
conn.close
Set rs = Null
Set conn = Null
%>
<% SUB List() %>
<% session("referer" = Request.ServerVariables("HTTP_REFERER" %>
<FORM METHOD="POST" ACTION="quiz.asp">
<OL>
<% DO WHILE NOT rs.eof %>
<LI><B><%= rs("question"%></B><BR>
<%
FOR i = 1 to 5
choice = rs.fields(i+1).value
IF choice <> "" THEN
%>
<INPUT TYPE="RADIO" NAME="<%= rs("questionID" %>" VALUE="<%= i %>">
<%= choice %>
<BR>
<%
END IF
NEXT
%>
<BR>
<%
rs.MoveNext
LOOP
%>
</OL>
<P><INPUT TYPE="SUBMIT" VALUE="Submit quiz for scoring">
<INPUT TYPE="RESET" VALUE="Clear All Answers">
</FORM>
<% END SUB %>
<%
SUB Mark()
correctCount = 0
total = 0
%>
<HTML>
<BODY BGCOLOR="#ffffff">
<%
FOR EACH x IN Request.Form
DO WHILE rs("questionID" <> CInt(x)
rs.MoveNext
LOOP
correctID = rs("correctID"
chosenID = CInt(Request.Form(x))
%>
<P>
<B>The question was: </B><%= rs("question" %><BR>
<B>Your choice was: </B><%= rs.fields(chosenID+1).value %>.<BR>
<%
IF chosenID = correctID THEN
correctCount = correctCount + 1
%>
<B>Your answer is correct.</B><BR>
<%
ELSE
%>
<B>The correct answer is: </B><%= rs.fields(correctID+1).value %>.<BR>
<B>Feedback: </B><%= rs("comment" %>.<BR>
<%
END IF
NEXT
rs.MoveFirst
DO WHILE NOT rs.eof
total = total + 1
rs.MoveNext
LOOP
%>
<P>You answered correctly <%= correctCount %> / <%= total %> questions
and your score is <B><%= Round(correctCount / total * 100) %>%</B>
<P><A HREF="<%= session("referer" %>">End Quiz.</A>
</BODY>
</HTML>
<% END SUB %>
The following piece of code is based on a quiz.
It currently takes 'all' the records (questions and 4 corresponding answer choices)
from the databaseand displays them on a web page.
The database is made up of 6 fields, 1 question, 4 answer and 1 correctID field that
holds the number that corresponds to the correct answer position in each case.
When the quiz is completed the SUB Mark() function calculates the score and displays questions,
correct answers and the score on another page.
What I wanted to know was how I could randomize the quiz.
i.e Retrieve 10 random questions from a database table of 30 thirty questions
and display them and their answers on a page?? (The file is called quiz.asp)
Any help and suggestions would be greatrly appreciated!!
Thanks,
Portmoon.
<%
' On Error Resume Next
Set Conn = Server.CreateObject("ADODB.Connection"
Conn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("myDatabase.mdb"
SQL = "SELECT * Quiz, conn, 3"
Set rs = conn.Execute(SQL)
IF Request.Form.Count = 0 THEN
CALL List
ELSE CALL Mark
END IF
rs.close
conn.close
Set rs = Null
Set conn = Null
%>
<% SUB List() %>
<% session("referer" = Request.ServerVariables("HTTP_REFERER" %>
<FORM METHOD="POST" ACTION="quiz.asp">
<OL>
<% DO WHILE NOT rs.eof %>
<LI><B><%= rs("question"%></B><BR>
<%
FOR i = 1 to 5
choice = rs.fields(i+1).value
IF choice <> "" THEN
%>
<INPUT TYPE="RADIO" NAME="<%= rs("questionID" %>" VALUE="<%= i %>">
<%= choice %>
<BR>
<%
END IF
NEXT
%>
<BR>
<%
rs.MoveNext
LOOP
%>
</OL>
<P><INPUT TYPE="SUBMIT" VALUE="Submit quiz for scoring">
<INPUT TYPE="RESET" VALUE="Clear All Answers">
</FORM>
<% END SUB %>
<%
SUB Mark()
correctCount = 0
total = 0
%>
<HTML>
<BODY BGCOLOR="#ffffff">
<%
FOR EACH x IN Request.Form
DO WHILE rs("questionID" <> CInt(x)
rs.MoveNext
LOOP
correctID = rs("correctID"
chosenID = CInt(Request.Form(x))
%>
<P>
<B>The question was: </B><%= rs("question" %><BR>
<B>Your choice was: </B><%= rs.fields(chosenID+1).value %>.<BR>
<%
IF chosenID = correctID THEN
correctCount = correctCount + 1
%>
<B>Your answer is correct.</B><BR>
<%
ELSE
%>
<B>The correct answer is: </B><%= rs.fields(correctID+1).value %>.<BR>
<B>Feedback: </B><%= rs("comment" %>.<BR>
<%
END IF
NEXT
rs.MoveFirst
DO WHILE NOT rs.eof
total = total + 1
rs.MoveNext
LOOP
%>
<P>You answered correctly <%= correctCount %> / <%= total %> questions
and your score is <B><%= Round(correctCount / total * 100) %>%</B>
<P><A HREF="<%= session("referer" %>">End Quiz.</A>
</BODY>
</HTML>
<% END SUB %>