Apologies for posting a big chunk of code. This is a simple multiple-choice
quiz that gets generated from a tab-delimited text file as noted below. It
works, but as a novice CF programmer, I was wondering how it might be improved.
Thanks,
Trey
====================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
<!---
FileName: SimpleQuiz
Version: 1.0
Created by: Trey Beck <[EMAIL PROTECTED]>
Created on: 3-April-2002
Notes: This file reads a tab-delimited text file to create
a simple multiple choice quiz. The text file should be in the format (w/o
quotes) "answer1 [tab]answer2 [tab]answer3 [tab]correctanswer
[tab]question" where "correctanswer" is the position of the correct answer
in the list (eg 1, 2 or 3). The form submits to itself.
In order to modify this form for reuse, change the value for QuizData and
change the filename that the form is submitting to as appropriate.
--->
<!--- If form has not been submitted, display it --->
<cfif NOT isdefined("form.fieldnames")>
<!--- Set name of data file --->
<cfset QuizData = "C:\inetpub\wwwroot\quiz.txt">
<!--- Initialize new values --->
<cfset LineNumber = 0>
<cfset ItemNumber = 1>
<cfset QuizArray = ArrayNew(2)>
<!--- Read tab-delimited data file --->
<cffile action="read" file="#QuizData#" variable="AllLines">
<!--- Loop over individual lines --->
<cfloop list="#AllLines#" delimiters="#chr(13)#" index="SingleLine">
<!--- Initialize LineNumber, which counts lines --->
<cfset LineNumber = LineNumber + 1>
<!--- The number of answers should be the number of items
minus two (the correct answer and the question) --->
<!--- Assume that all questions have the same number of
answers --->
<cfset NumberOfItems = (#ListLen(SingleLine,"#chr(9)#")#)>
<cfset NumberOfAnswers = (NumberOfItems - 2) >
<!--- Loop over individual items within the line --->
<cfloop list="#SingleLine#" delimiters="#chr(9)#"
index="Item">
<!--- Add items to the array. The first values
should be answers, then the correct answer, then the question --->
<cfset QuizArray [#LineNumber#][#ItemNumber#] =
"#Item#">
<cfset ItemNumber = ItemNumber + 1>
</cfloop>
<!--- Reset ItemNumber so that reading the next line into
the array starts at position one --->
<cfset ItemNumber = 1>
</cfloop>
<!--- The value of LineNumber should now be the total number of
lines, assuming that there are no blank lines --->
<cfset NumberOfQuestions = LineNumber>
<!--- Begin form. It will post to this page, which should be
defined by "Self" --->
<cfform name="Quiz" action="thisfile.cfm" method="post">
<!--- Display the quiz. The question will always be the last item
in a line from the data file --->
<cfloop from="1" to="#NumberOfQuestions#" index="QuestionIndex">
<!--- Display question --->
<cfoutput><p>#QuizArray[QuestionIndex][NumberOfItems]#<br></cfoutput>
<!--- Display answers --->
<cfloop from="1" to="#NumberOfAnswers#" index="ItemIndex">
<cfoutput><cfinput type="radio"
value="#ItemIndex#" name="Ans#QuestionIndex#" required="Yes" message="You
must complete the entire quiz. Please check your work and try
again.">#QuizArray[QuestionIndex][ItemIndex]#<br></cfoutput>
</cfloop>
</cfloop>
<p></p>
<input type="submit" value="Send"> <input type="reset" value="Clear">
</cfform>
<!--- If form has been submitted, process it --->
<cfelse>
<!--- Normally, check to see if form fields are defined. In this
case, validation occurs at the form --->
<!--- Might want to reassign form field values to local variables --->
<!--- This next bit is the same as above. Using session or url
scope would obviate recreating the array. Future project --->
<cfset QuizData = "C:\inetpub\wwwroot\quiz.txt">
<!--- Initialize new values --->
<cfset LineNumber = 0>
<cfset ItemNumber = 1>
<cfset QuizArray = ArrayNew(2)>
<!--- Read tab-delimited data file again --->
<cffile action="read" file="#QuizData#" variable="AllLines">
<!--- Loop over individual lines --->
<cfloop list="#AllLines#" delimiters="#chr(13)#" index="SingleLine">
<!--- Initialize LineNumber, which counts lines --->
<cfset LineNumber = LineNumber + 1>
<!--- The number of answers should be the number of items
minus two (the correct answer and the question) --->
<!--- Assume that all questions have the same number of
answers --->
<cfset NumberOfItems = (#ListLen(SingleLine,"#chr(9)#")#)>
<cfset NumberOfAnswers = (NumberOfItems - 2) >
<!--- Loop over individual items within the line --->
<cfloop list="#SingleLine#" delimiters="#chr(9)#"
index="Item">
<!--- Add items to the array. The first values
should be answers, then the correct answer, then the question --->
<cfset QuizArray [#LineNumber#][#ItemNumber#] =
"#Item#">
<cfset ItemNumber = ItemNumber + 1>
</cfloop>
<!--- Reset ItemNumber so that reading the next line into
the array starts at position one --->
<cfset ItemNumber = 1>
</cfloop>
<!--- The value of LineNumber should now be the total number of
lines, assuming that there are no blank lines --->
<cfset NumberOfQuestions = LineNumber>
<cfset NumberRight = 0>
<!--- Display the answers to the quiz --->
<cfloop from="1" to="#NumberOfQuestions#" index="QuestionIndex">
<!--- Remember given & correct answers. Correct answer
number will be the second-to-the-last position (here, 4) --->
<!--- Evaluate('Form.Ans'&'#QuestionIndex#') should yield
something like "Form.Ans2", the form field submitted --->
<cfset CurrentAnswerNumber =
Evaluate('Form.Ans'&'#QuestionIndex#')>
<cfset CurrentAnswer =
QuizArray[QuestionIndex][CurrentAnswerNumber]>
<cfset CorrectAnswerNumber = QuizArray[QuestionIndex][4]>
<cfset CorrectAnswer =
QuizArray[QuestionIndex][Evaluate(QuizArray[QuestionIndex][4])]>
<cfset CurrentQuestion = QuizArray[QuestionIndex][5]>
<!--- Display question, answer and correct answer if
necessary --->
<cfoutput>
<p>#CurrentQuestion#<br>
You chose #CurrentAnswer#.<br>
<cfif CurrentAnswerNumber EQ CorrectAnswerNumber>
That is correct!</p>
<cfset NumberRight = NumberRight + 1>
<cfelse>
That is incorrect. The correct answer is
#CorrectAnswer#</p>
</cfif>
</cfoutput>
</cfloop>
<cfset Score = (NumberRight/NumberOfQuestions)*100>
<cfoutput><p>You scored #Score#%. Thanks for taking the
quiz!</p></cfoutput>
</cfif>
</body>
</html>
______________________________________________________________________
Structure your ColdFusion code with Fusebox. Get the official book at
http://www.fusionauthority.com/bkinfo.cfm
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Archives: http://www.mail-archive.com/[email protected]/
Unsubscribe: http://www.houseoffusion.com/index.cfm?sidebar=lists