Solved

How do you use multiple variables in loops?

Posted on 2008-10-22
3
169 Views
Last Modified: 2012-05-05
I have a quiz with the information (ID [the auto-incremented primary], question, a [answer a], b [answer b], c [answer c], d [answer d], and correct [the correct answer; a, b, c, or d]) in a database. To output the quiz I used a simple loop over a query that gets all the questions:

<cfset i=1>
<table>
      <cfloop query="qGetQuestions">
            <tr>
                  <td>#i#.</td>
                  <td>#question#<br />
                        <table>
                              <tr>
                                    <td><input name="#ID#" type="radio" value="a"></td>
                                    <td>#a# </td>
                              </tr>
                              <tr>
                                    <td><input name="#ID#" type="radio" value="b"></td>
                                    <td>#b# </td>
                              </tr>
                              <tr>
                                    <td><input name="#ID#" type="radio" value="c"></td>
                                    <td>#c#</td>
                              </tr>
                              <tr>
                                    <td><input name="#ID#" type="radio" value="d"></td>
                                    <td>#d#</td>
                              </tr>
                        </table></td>
                  </tr>
            <cfset i = i + 1>
                  </cfloop>
                  <tr>
                        <td></td>
                        <td><input type="submit" name="Submit" value="Submit" /></td>
                  </tr>
</table>

Not a problem. The problem comes when trying to score. If I loop through the query again, I need to check and see if #qGetQuestions.correct# is equal to #FORM.#ID##, which is obviously not a valid variable. How do I go about this, or have I been going about the test setup incorrectly from the start?

Thanks!
0
Comment
Question by:SiobhanElara
  • 2
3 Comments
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 22777786
Check this:
<cfset i=1>

<cfset Correct=0>

<cfset subValue="" >

<table>

 <cfloop query="qGetQuestions">

  <tr>

   <td>#i#.</td>

   <td>#question#<br />

   <cfif IsDefined("FORM.FieldNames") >

     <cfset subValue=FORM[ID] >

   </cfif>

   <table>

   <tr>

    <td><input name="#ID#" type="radio" value="a" <cfif subValue EQ "a">CHECKED</cfif> ></td>

    <td>#a# </td>

   </tr>

   <tr>

    <td><input name="#ID#" type="radio" value="b" <cfif subValue EQ "b">CHECKED</cfif> ></td>

    <td>#b# </td>

   </tr>

   <tr>

    <td><input name="#ID#" type="radio" value="c" <cfif subValue EQ "c">CHECKED</cfif> ></td>

    <td>#c#</td>

   </tr>

   <tr>

    <td><input name="#ID#" type="radio" value="d" <cfif subValue EQ "d">CHECKED</cfif> ></td>

    <td>#d#</td>

   </tr>

   </table></td>

  </tr>

  <cfset i = i + 1>

  <cfif IsDefined("FORM.FieldNames") >

    <cfif FORM[ID] EQ answer >

      <cfset Correct=Correct+1>

    </cfif>

  </cfif>

 </cfloop>

 <cfif IsDefined("FORM.FieldNames") >

   <tr><td colspan="2">Correct answers: #Correct#</td</tr>

 </cfif>

 <tr>

 <td></td>

 <td><input type="submit" name="Submit" value="Submit" /></td>

 </tr>

</table>

Open in new window

0
 

Author Comment

by:SiobhanElara
ID: 22778287
Thanks for your response! While it does appear that the above code would tell you how many questions were correct, it doesn't seem to tell them *which* ones they got right and what the correct answer would be. (I should have specified that was what I was going for but was hoping to be able to figure that part out if the double variable thing got explained.) What I would like it to do, in the end, is to say (using explanatory variable names for clarity):

You got #numberCorrect# of #totalQuestions# questions correct!

1. #question#
    The correct answer is #textOfCorrectAnswer#. You answered #textOfExamAnswer#.

Etc.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 22779082
Open a new question please for that formating.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now