|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I'm trying to add values together based on a criteria. If an answer is wrong make 0, if its right, make it equal to the weight of the question. Here is the code I have so far.
Code:
<cfoutput query="answers">
<cfif answers.answer EQ answers.correctanswer>
<cfset correct = answers.weight>
</cfif>
<cfif answers.answer NEQ answers.correctanswer>
<cfset wrong = 0 - answers.weight>
</cfif>
<cfset totalscore = 0>
<cfloop query="answers">
<cfset correctscore = totalscore + correct />
<cfset wrongscore = totalscore + wrong />
</cfloop>
<cfset score = correctscore + wrongscore />
</cfoutput>
Please Help! I am not an expert with coldfusion and I've had no official training. I have no idea if I am going about this right. Any help would be much appreciated. ![]() Last edited by nhrrdan : December 23rd, 2004 at 01:23 PM. Reason: Typo |
|
#2
|
|||
|
|||
|
What you're doing (looping over the query twice) and comparing the query's answer (where did that come from?) to the query's correct answer doesn't make sense. Can you explain in more detail what you are trying to do? Is the answer coming from a form?
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian. How to Post a Question in the Forums |
|
#3
|
|||
|
|||
|
I'm going to display the answers on a page for the user to see which ones were wrong, I also want the page to calculate a grade. Each question has a weight (i.e. 1-5 are worth 10, while 6-15 are worth 5 each) the reason for this is that 1-5 are easier questions and should be known by the test taker.
If the question is wrong I want the value to be a negative of that questions weight (I.e. -10). If the question is correct then I want the weight of the answer to be positive. After all these values are displayed, I then want combine them together. Is there another way to approach this. I tried using the <cfloop> but have had no luck. I hope this is more descriptive and thanks for the response. Quote:
|
|
#4
|
|||
|
|||
|
But where are the user's answers coming from? A form? The query?
|
|
#5
|
|||
|
|||
|
I'm sorry, they are coming from a query of an Access database table.
Quote:
|
|
#6
|
|||
|
|||
|
I've simplified things a bit.
I want to add up all the wrong values and subtract it from the total possible score. I am looping through a query of the database. I can't seem to get it to work, I have no Idea how to add up the wrong values in the line marked with **. All I get is the last value the loop generates, instead of adding up the values. I know how to use aggregate functions in the SQL query, but is this possible? Or, is there something I have to do with an array? Someone please help. Code:
<cfloop query="answers"> <cfif answers.answer NEQ answers.correctanswer> <cfset wrong = 0 - #answers.weight#> **<cfset totalwrong = ???????????> </cfif> </cfloop> |
|
#7
|
|||
|
|||
|
Are you looking for something like this:
<cfset totalWrong = 0 /> <cfloop query="answers"> <cfif answers.answer NEQ answers.correctanswer> <cfset wrong = 0 - #answers.weight# /> <cfset totalWrong = totalWrong + wrong /> ... |
|
#8
|
|||
|
|||
|
Sorry for the delay on replying. I was on vacation last week and didn't use a computer for the entire week. I haven't tried your recomendation yet, but I will give it a shot. I appreciate your response. Thank you.
Quote:
|
|
#9
|
|||
|
|||
|
I just tried plugging that in to my code and it worked. Thank you very much, you have saved me quite a bit of time.
![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > Dynamic Addition with multile fields |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|