===========
Index2.cfm
===========


<!---/// 
My comments are between "<!---///" and "///--->"

I removed all of your comments

one of the main things i change is that the delimeter in the file is now "|"
instead of a tab. I think that that tabs
and spaces for delimeters are clunky to work with. Also I seperated the
question and answers with a "^".

 ///--->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
        <title>Untitled</title>
</head>
<body>

<cfset thisfile = GetFileFromPath(GetBaseTemplatePath())>

<!---/// Try to put common code in a common area ///--->
<!---/// 
You could use some function and CFFILE so that all they would have to do is
to put the quiz.txt in the same directory as the page ///--->   
<cffile action="READ"
        file="#GetDirectoryFromPath(GetBaseTemplatePath())#quiz.txt"
        variable="AllLines">
                        
<!---/// use cfscript and functions to make code faster ///--->
<cfscript>
        LineNumber = 1;
        QuizArray = ArrayNew(2);
        NumberOfQuestions = ListLen("#AllLines#", "#chr(10)##chr(13)#");
        for(SingleLine=1;SingleLine LTE
NumberOfQuestions;SingleLine=SingleLine+1){
                ThisLine = ListGetAt(AllLines, SingleLine,
"#chr(10)##chr(13)#");
                AnswerList = ListFirst(ThisLine, "^");
                Question = ListLast(ThisLine, "^");
                QuizArray[LineNumber][1] = AnswerList;
                QuizArray[LineNumber][2] = Question;
                LineNumber = LineNumber + 1;
        }
</cfscript>

<!--- If form has not been submitted, display it --->
<cfif NOT isdefined("form.fieldnames")>

        <cfform name="Quiz" action="#thisfile#" method="post">
                <cfloop from="1" to="#NumberOfQuestions#"
index="QuestionIndex">
        
<cfoutput><p>#QuizArray[QuestionIndex][2]#<br></cfoutput>
                        <cfset Answers =
ListLen(QuizArray[QuestionIndex][1], '|') -1>
                        <cfloop from="1" to="#Answers#" index="ItemIndex">
                                <cfoutput><cfinput type="radio"
value="#ListGetAt(QuizArray[QuestionIndex][1], ItemIndex, "|")#"
name="Ans#QuestionIndex#" required="Yes" message="You must complete the
entire quiz. Please check your work and try
again.">#ListGetAt(QuizArray[QuestionIndex][1], ItemIndex,
"|")#<br></cfoutput>
                        </cfloop>
                </cfloop>
                <p></p>
                <input type="submit" value="Send"> <input type="reset"
value="Clear">
        </cfform>

<cfelse>

        <cfscript>
                NumberRight = 0;
                for(QuestionIndex=1;QuestionIndex LTE
NumberOfQuestions;QuestionIndex=QuestionIndex+1){
                        YourAnswer = Evaluate("Form.Ans"&QuestionIndex);
                        CorrectAnswer =
#ListLast(QuizArray[QuestionIndex][1], "|")#;
                        writeoutput("<p>Question #QuestionIndex#:
#QuizArray[QuestionIndex][2]#<br>");
                        writeoutput("Correct Answer: #CorrectAnswer#<br>");
                        if(YourAnswer EQ CorrectAnswer){
                                writeoutput("That is correct!</p>");
                                NumberRight = NumberRight + 1;
                        }
                        else{writeoutput("That is incorrect.</p>");}
                }
                Score = (NumberRight/NumberOfQuestions)*100;
                writeoutput("<p>You scored #Score#%. Thanks for taking the
quiz!</p>");
        </cfscript>

</cfif>

</body>
</html>




========
Quiz.txt
========
me|greg|charles|me^who's you daddy
tony|charles|greg|tony^who is the man
kara|peach|jewlery|nutmeg|peach^cutest dog



Anthony Petruzzi
Webmaster
954-321-4703
http://www.sheriff.org


-----Original Message-----
From: trey [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, April 03, 2002 2:50 PM
To: CF-Talk
Subject: How could I improve this code?


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>



______________________________________________________________________
Signup for the Fusion Authority news alert and keep up with the latest news in 
ColdFusion and related topics. http://www.fusionauthority.com/signup.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

Reply via email to