| |
Portmoon
Posts: 4
Score: 0
Joined: 2/24/2003
From:
Status: offline
|
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") %> <% END SUB %> <% SUB Mark() correctCount = 0 total = 0 %> <% FOR EACH x IN Request.Form DO WHILE rs("questionID") <> CInt(x) rs.MoveNext LOOP correctID = rs("correctID") chosenID = CInt(Request.Form(x)) %> The question was: <%= rs("question") %> Your choice was: <%= rs.fields(chosenID+1).value %>. <% IF chosenID = correctID THEN correctCount = correctCount + 1 %> Your answer is correct. <% ELSE %> The correct answer is: <%= rs.fields(correctID+1).value %>. Feedback: <%= rs("comment") %>. <% END IF NEXT rs.MoveFirst DO WHILE NOT rs.eof total = total + 1 rs.MoveNext LOOP %> You answered correctly <%= correctCount %> / <%= total %> questions and your score is <%= Round(correctCount / total * 100) %>% ">End Quiz. <% END SUB %>
|
|