⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 quiz.asp

📁 网上考试系统 随着社会的发展
💻 ASP
📖 第 1 页 / 共 2 页
字号:
<%
'*******************************************************
'*     ASP 101 Sample Code - http://www.asp101.com     *
'*                                                     *
'*   This code is made available as a service to our   *
'*      visitors and is provided strictly for the      *
'*               purpose of illustration.              *
'*                                                     *
'* Please direct all inquiries to webmaster@asp101.com *
'*******************************************************
%>

<%
' Takes a integer parameter and converts it to the appropriate letter
Function GetLetterFromAnswerNumber(iInput)
Dim strTemp
	Select Case iInput
		Case 0
			strTemp = "A"
		Case 1
			strTemp = "B"
		Case 2
			strTemp = "C"
		Case 3
			strTemp = "D"
		Case 4
			strTemp = "E"
		Case 5
			strTemp = "F"
	End Select
GetLetterFromAnswerNumber = strTemp
End Function

' To simplify and streamline the code I split this line into many parts,
' wrapped it into a function and commented it so you'd have a better
' chance of figuring out what I'm doing since I usually can't!
Function GetAnswerFromAnswerString(iQuestionNumber, strAnswers)
Dim strTemp
Dim iOffset
	' I use InStrRev since I want to retrieve the last entered value
	' in case they went back and changed their mind. 
	
	' Find the location of the question number we want to use
	iOffset = InStrRev(strAnswers, "|" & iQuestionNumber & "|", -1, 1)
	
	' Get our answer by using the offset we just found and then moving
	' right the length of the question indicator to arrive at the
	' appropriate letter
	strTemp = Mid(strAnswers, iOffset + Len("|" & iQuestionNumber & "|"), 1)

	' There's no way it should be anything else, but to be sure we
	' convert it to a string and make sure it's uppercase
GetAnswerFromAnswerString = UCase(CStr(strTemp))
End Function
%>

<%
' This code works with either a DB or hard coded in values.  I developed
' it using the DB connection and our sample DB, but switched to the hard
' coded to increase the speed for our site.  Using the DB allows for better
' data encapsulation and easier updates.  It all comes down to your specific
' needs so I gave you both options.

' Set this to True to use the Db.  False to use hard coded values
Const USE_DB_FOR_INFO = False

' These 2 things only apply if you use the DB
' If you're not then they're values are irrelevant
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;"
' Lets you run multiple quizzes from one DB by separating by QUIZ_ID!
Const QUIZ_ID = 1


' Now to all our other variables!
Dim cnnQuiz, rsQuiz     'DB objects if we use the DB for the info

Dim I                   'our standard (improperly named) looping variable
Dim iNumberOfQuestions  'the number of questions in the test
Dim iQuestionNumber     'the question we're currently on
Dim strQuestionText     'text of the question to be asked
Dim aAnswers            'array of choices for the question to be asked
                        'if we hard code, then I also use it for the
                        'correct answers when I go to grade the quiz
Dim strAnswers          'stores the question numbers and response choices
                        'seperated by |'s
Dim iScore              'so we know how well the user did
Dim bAbort              'added after I had finished to account for closed sessions
Dim strResults          'another late addition for the each question breakdown!

bAbort = False          'set it to false since we only want to abort in certain cases



' If this is the first call to the quiz then init everything
' o/w retrieve values we need.  We check by looking for the
' Question ID from the querystring.
If Request.QueryString("qid") = "" Then
	' Retrieve and Set the Quiz Info
	If USE_DB_FOR_INFO Then
		' Code to use DB!
		' Create DB connection and connect to the DB
		Set cnnQuiz = Server.CreateObject("ADODB.Connection")
		cnnQuiz.Open DB_CONN_STRING
		
		' Create RS and query DB for quiz info
		Set rsQuiz = Server.CreateObject("ADODB.Recordset")
		rsQuiz.Open "SELECT * FROM quizzes WHERE quiz_id=" & QUIZ_ID & ";", cnnQuiz
		
		' Set our session vars
		Session("QuizName") = CStr(rsQuiz.Fields("quiz_name").Value)
		Session("NumberOfQuestions") = CInt(rsQuiz.Fields("number_of_questions").Value)
		Session("PercentageToPass") = CInt(rsQuiz.Fields("percentage_to_pass").Value)
		
		' Close and dispose of our DB objects
		rsQuiz.Close
		Set rsQuiz = Nothing
		cnnQuiz.Close
		Set cnnQuiz = Nothing
	Else
		' If we're not going to the DB, hard code in values here!
		' BEGIN HARD CODE
		Session("QuizName") = "ASP 101 Quiz"
		Session("NumberOfQuestions") = 10
		Session("PercentageToPass") = 70
		' END HARD CODE
	End If

	' Set our question indicator to 1 and init our answer string
	iQuestionNumber = 1
	Session("AnswerString") = "|"
Else
	'Check to be sure we've still got a session!
	If Session("AnswerString") = "" Then
		Response.Write "I'm sorry, but you've taken too long.  You can start over by "
		Response.Write "clicking <A HREF=""" & Request.ServerVariables("URL") & """>here</A>."
		' I'd normally just do a response.end, but I can't because I'm inside of our
		' site template.  I need the script to complete so I've declared and set a Flag
		'Response.End
		bAbort = True
	End If
	
	' Get the number of the question we're processing
	iQuestionNumber = CInt(Request.QueryString("qid"))
		
	' Log selected answer to last question
	Session("AnswerString") = Session("AnswerString") & iQuestionNumber & "|" & _
		GetLetterFromAnswerNumber(CInt(Request.QueryString("sa"))) & "|"
	
	' Increment question identifier
	iQuestionNumber = iQuestionNumber + 1
End If

' If session has expired then skip all the code.
' Equivalently, only run all the code if it hasn't!
If Not bAbort Then

	' Set this to a local variable to avoid accessing the collection each time
	' This isn't required, but makes it easier for me to access and
	' supposedly speeds it up... I'm not sure how much, but it can't hurt!
	iNumberOfQuestions = Session("NumberOfQuestions")

	' Check to see it the quiz is over.  If so then show results, o/w
	' ask the next question
	If iQuestionNumber > iNumberOfQuestions Then
		' Process results and show end quiz status report
		
		' Done for the same reason as for iNumberOfQuestions a few lines above
		strAnswers = Session("AnswerString")
		
		' Useful for debugging
		'Response.Write strAnswers & "<BR>" & vbCrLf & "<BR>" & vbCrLf

		' Bug hunting once again... I didn't even come across any real bugs on this trip!
		' Could you imagine the ammo I'd take if it was real hunting I was doing!
		'For I = 1 to iNumberOfQuestions
		'	Response.Write GetAnswerFromAnswerString(I, strAnswers) & "<BR>" & vbCrLf
		'Next 'I

		' Retrieve Correct Answers and compare to the entered ones
		If USE_DB_FOR_INFO Then
			' Code to use DB!
			' Create DB connection and connect to the DB
			Set cnnQuiz = Server.CreateObject("ADODB.Connection")
			cnnQuiz.Open DB_CONN_STRING
				
			' Create RS and query DB for quiz info
			Set rsQuiz = Server.CreateObject("ADODB.Recordset")
			' Specify 3, 1 (Static, Read Only)
			rsQuiz.Open "SELECT * FROM questions WHERE quiz_id=" & QUIZ_ID & _
				" ORDER BY question_number;", cnnQuiz, 3, 1
				
			iScore = 0
			I = 1
			Do While Not rsQuiz.EOF
				If UCase(CStr(rsQuiz.Fields("correct_answer").Value)) = _
					GetAnswerFromAnswerString(I, strAnswers) Then
					
					iScore = iScore + 1
					' This and the Else could be used to output a
					' correctness status for each question
					' Also useful for bug hunting!
					'Response.Write "Right" & "<BR>" & vbCrLf
				Else
					'Response.Write "Wrong" & "<BR>" & vbCrLf
					strResults = strResults & I & ", "
				End If
				I = I + 1
				rsQuiz.MoveNext
			Loop

			' Close and dispose of our DB objects
			rsQuiz.Close
			Set rsQuiz = Nothing
			cnnQuiz.Close
			Set cnnQuiz = Nothing
		Else
			' If we're not going to the DB, hard code in answer values here!
			' BEGIN HARD CODE
			aAnswers = Array("A", "A", "A", "E", "D", "A", "E", "E", "A", "A")
			' END HARD CODE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -