I am working on an interface that communicates with a PostgresSQL to create and grade a multiple choice test. The steps go like this:
1. Get user information
2. Set up a record of the user's test in the database
3. Pull in questions and possible responses from tables.
4. Create a form populated with the results of the query from step #3.
5. Pass form inputs to a handler script that inserts a record for each response.
6. Run a query to compare the user's responses with the possible responses from step #3.
7. Find number of correct responses, calculate score.
8. Display user's responses and whether they were correct or not.
The problem I am running into is in step #5. As there are going to end up being over 100 questions, I want to set the form up dynamically. The approach I am using to create the form is to loop the output of the query that brings in the questions and responses. The input for each response is a radio button defined as such:
<input type="radio" name="q#question_number#" value="#response_letter#">
This will yield radio button names ranging from 1 to however many questions there are. When these are passed to the handler script, I want to run the results through a loop that inserts a record for each response until the number of inputs is exhausted. The problem I am having is in how to define this for the loop. Right now, this is what I've come up with:
<cfloop index="question_number" from="1" to="#q_count#"> <cfset resp = form.q#question_number#> <cfquery name="add_resp" datasource="..."> INSERT INTO user_responses (perstest_id, question_number, user_response, date_entered) VALUES ('#perstest#', '#i#', '#resp#', '#today_date#') </cfquery> </cfloop>
#q_count# was defined from the record count of a query to count how many questions there are for that test.
#perstest# was defined from a query that pulled in the identifier for this test from the table recording the tests taken by the user.
After trying a number of different approaches, from populating an array to various syntax methods, I come up with the same problem: how do I define the form variables dynamically? If there isn't a short answer to this, I'd appreciate recommendations on tutorials or other documentation that would answer this for me.