I am using ColdFusion 9.1. I am coding using CFSCRIPT.
I am creating a quiz. Query one will go get an image of a person and the person of the person in the image. This will be the correct answer. Query two will go get two other people who are not in the image. I want to put this into an array and then sort the array so that the correct answer isn’t always at the top or the bottom or in the middle.
Here is my pseudo SQL:
QUERY ONE NAME = GET CORRECT ANSWER
SELECT TOP 1 ImageID, PersonID, FirstName, LastName
FROM IMAGES
QUERY TWO NAME = GET TWO INCORRECT ANSWERS
SELECT TOP 2 PersonID, FirstName, LastName
FROM IMAGES
WHERE ImageID IS NOT THE CORRECT ANSWER
I “think” I want my array to look like this:
PersonID="1234";
FirstName="Bob";
LastName="Jones";
I need to loop through each query and populate this array with the three people returned from the query. Like this, but this code doesn’t work:
<cfscript>
PersonArray = arrayNew(1);
for (i = 1; i lte GetTwoWrong.RecordCount; i++) {
Person = structNew();
Person.PersonID = GetTwoWrong.PersonID[i];
Person.FirstName = GetTwoWrong.FirstName[i];
Person.LastName = GetTwoWrong.LastName [i];
PersonArray = arrayAppend(PersonArray , Person);
}
</cfscript>
Then I need to sort the array by FirstName, LastName, or PersonID to create any kind of randomness.
<cfscript>
PersonArray = arraySort(PersonArray numeric);
</cfscript>
Then I will need to output the answers. The answer will be clickable. On click, I will send the PersonID via jQuery to check the answer for correctness (and store the selection).
So, how do I create an array and then populate it from two different queries and then sort it? Should I be using an array at all? A structure?
I would do this in the database, which will be far less complicated. Here’s an example:
The two queries on either side of the
UNIONcan be totally different against different databases, tables, and criteria, so long as they both return the same columns. You’ll notice in the second query, I’m returning a NULL value for something that doesn’t exist there, but does exist in the first one (just an example). So you can see how you can return different values from each side of theUNIONand get them in a completely random order.This is something that database servers are much better at than application code. If you need to (somewhere on the page) deal with your correct and incorrect answers differently, you can then use a Query of Query to pull just the correct or incorrect answers out of the base query.