Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems displaying answer choices in quiz??

Status
Not open for further replies.

Portmoon

Programmer
Feb 24, 2003
11
0
0
MY
Hi there,

I'm attempting to construct random based quiz but can't seem to be able to display the four answer choices, nor can I provide feedback to the user. Any help would be greatly appreciated. I have given a brief synopsis below and hopefully I've been clear enough.

Thanks for your time,

James





The program displays 10 randomly selected questions from a database table of 30 records (questions).
This is done by loading all the questions and associated answers into a number of arrays. A random number (shuffler) is allocated to each question and it's answers are then sorted using a bubble sort to prevent the questions occuring more than once during the same quiz.
I can get the questions to be displayed randomly but can't display the four answer choices??

When the quiz is completed I also the need the code to calculate the user's score which will then be displayed on another web page once executed by the server alongside the question asked and the correct answer if the user gets the question wrong.

The database contains 7 fields - Quiz ID(Autonumber relating to each question), 1 question field, 4 answer fields(Answer1, Answer2 etc) and a CorrectAns field that holds an integer relating to the correct answer position.


The asp code is as follows:




<% On Error Resume Next
Set Conn = Server.CreateObject(&quot;ADODB.Connection&quot;)
Conn.Open &quot;DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=&quot; & Server.MapPath(&quot;myDatabase.mdb&quot;) & &quot;;pwd=myPassword&quot;

'connect to database

set rs = Server.CreateObject(&quot;adodb.RecordSet&quot;)
rs.open &quot;SELECT SixtiesQuiz.* FROM SixtiesQuiz&quot;, conn, 3


'get a count of records returned
rs.PageSize = 1
size = rs.PageCount


' re-dimension arrays to correct size
redim question(size)
redim answer1(size)
redim answer2(size)
redim answer3(size)
redim answer4(size)
redim shuffler(size)



if size > 0 then
index = 0
do while not rs.eof


question(index) = rs(&quot;Question&quot;)
answer1(index) = rs(&quot;Answer1&quot;)
answer2(index) = rs(&quot;Answer2&quot;)
answer3(index) = rs(&quot;Answer3&quot;)
answer4(index) = rs(&quot;Answer4&quot;)


randomize 'Initializing the random number generator
shuffler(index) = Rnd 'returns a random value
index = index + 1
rs.movenext

loop
end if

'tidy up after yourself!
rs.close
conn.close
Set rs = Null
Set conn = Null

'<><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>

' bubble sort based on random value

maxCount = (size - 1)

for i = 0 to (maxCount) step 1

for j = 0 to (maxCount) step 1

if shuffler(j) < shuffler(j+1) then
temp = shuffler(j)
shuffler(j) = shuffler(j+1)
shuffler(j+1) = temp

temp = question(j)
question(j) = question(j+1)
question(j+1) = temp

temp = answer1(j)
answer1(j) = answer1(j+1)
answer1(j+1) = temp

temp = answer2(j)
answer2(j) = answer2(j+1)
answer2(j+1) = temp

temp = answer3(j)
answer3(j) = answer3(j+1)
answer3(j+1) = temp

temp = answer4(j)
answer4(j) = answer4(j+1)
answer4(j+1) = temp
end if
next
next
%>

<!--*********************************************************************************-->


<%IF Request.Form.Count = 0 THEN
CALL List
ELSE CALL Mark
END IF
%>

<% SUB List() %>

<FORM METHOD=&quot;POST&quot; ACTION=&quot;pretest.asp&quot;>

<% 'display the first 10 questions
for index = 0 to (9) step 1%>

<script>
var answer<%=index%> = '';

</script>
<tr>

<td><%=&quot;<b> Q&quot; & index+1 & &quot;.</b> &quot;%>&nbsp;</td> <!--Question numbers-->

<td><%=question(index)%> </td> <!--Questions-->






<BR>
<BR>
<%NEXT%>






<P><INPUT TYPE=&quot;SUBMIT&quot; VALUE=&quot;Submit quiz for scoring&quot;>
<INPUT TYPE=&quot;RESET&quot; VALUE=&quot;Clear All Answers&quot;>
</FORM>
<% END SUB %>



<%
SUB Mark()

correctCount = 0
total = 0
%>
<HTML>
<BODY BGCOLOR=&quot;#ffffff&quot;>
<%
FOR EACH x IN Request.Form
DO WHILE rs(&quot;questionID&quot;) <> CInt(x)
rs.MoveNext
LOOP
correctID = rs(&quot;correctID&quot;)
chosenID = CInt(Request.Form(x))
%>
<P>
<B>The question was: </B><%= rs(&quot;question&quot;) %><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>


<%
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=&quot;<%= session(&quot;referer&quot;) %>&quot;>End Quiz.</A>

</BODY>
</HTML>
<% END SUB %>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top