Hello, I am trying to get some info from 2 database tables using an array and i need to , effectivly, store an array in an array. I think this is called a 2 dimensional array. Here is the code so you can see what im doing.. <!--- start old poll displays---> <!--- Get all the questions---> <cfquery name="qgetpolls" datasource="#dsn#"> SELECT * FROM question where isactive=1 </cfquery> <!--- count the number of questions---> <cfoutput> <cfset numpolls = #qgetpolls.recordcount#> </cfoutput> <!--- Grab all of the questions---> <cfloop from="1" to="#numpolls#" index="i"> <cfset polls[i ] = StructNew()> <cfset polls[i ].question = #qgetpolls.question[i ]#> <cfset polls[i ].id = #qgetpolls.questionid[i ]#> <!--- using the id above get the matching answers from the answer table---> <cfquery datasource="#dsn#" name="getnumanswers"> SELECT * FROM answer WHERE questionid = #polls[i ].id# </cfquery> <cfset numanswers = #getnumanswers.recordcount#> <!--- loop through and get all of the answers---> <cfloop from="1" to="#numanswers#" index="a"> <cfset pollanswers[a] = StructNew()> <cfset pollanswers[a].answer =#getnumanswers.answer[a]#> </cfloop> <!--- How do i store the above pollanswers[a].answer in the polls array---> </cfloop> Code (markup): can anyone help me do what i want, i think i have explained it correctly thanks
Putting the answers in the polls structs is as simple as: <cfloop from="1" to="#numpolls#" index="i"> <cfset polls[i ] = StructNew()> <cfset polls[i ].question = #qgetpolls.question[i ]#> <cfset polls[i ].id = #qgetpolls.questionid[i ]#> <cfquery datasource="#dsn#" name="getnumanswers"> SELECT * FROM answer WHERE questionid = #polls[i ].id# </cfquery> <cfset numanswers = #getnumanswers.recordcount#> <cfset polls[i ].pollanswers = arrayNew(1)> <cfloop from="1" to="#numanswers#" index="a"> <cfset polls[i ].pollanswers[a] = StructNew()> <cfset polls[i ].pollanswers[a].answer =#getnumanswers.answer[a]#> </cfloop> </cfloop> But all this code is really unnecessary... Why do you want to replicate the db structure into coldfusion arrays and structs anyway? Run the following code and see the resulting query object - it is exactly what you are looking for in a much simpler form... <cfquery datasource="#dsn#" name="qryPollAnswers"> SELECT * FROM question INNER JOIN answer ON question.id = answer.questionid where question.isactive=1 </cfquery> <cfdump var=#qryPollAnswers#>