Skip to main content
July 29, 2009
Question

Looping Over a Query

  • July 29, 2009
  • 1 reply
  • 509 views

Hello. I have this page. My problem is that when the table is generated, the scores are only filled in for the first test column. I've tried rearranging the loops and outputs, but I can't seem to get it to output correctly. Any ideas? Thanks.

<cfquery name="testnames" datasource="AITE_Test_System">      SELECT Test_Name     FROM dbo.[Tests]     WHERE Class_ID = 'de8e2958-dffe-4db0-8b01-2447294db854'     ORDER BY Test_ID </cfquery> <cfquery name="tests" datasource="AITE_Test_System">      SELECT Test_ID     FROM dbo.[de8e2958-dffe-4db0-8b01-2447294db854]     ORDER BY Test_ID </cfquery> <cfquery name="students" datasource="AITE_Test_System">      SELECT Fname, Lname, AD_Username     FROM dbo.[Students]     WHERE A1_Class = 'de8e2958-dffe-4db0-8b01-2447294db854'     ORDER BY Lname, Fname </cfquery> <table width="100%" border="0" align="center">   <tr align="center">     <th scope="col">Last Name</th>     <th scope="col">First Name</th>     <cfoutput query="testnames">       <th scope="col">#Test_Name#</th>     </cfoutput> </tr>   <cfoutput query="students">     <tr align="center">     <td>#Lname#</td>     <td>#Fname#</td>       <cfloop query="tests">      <cfquery name="scores" datasource="AITE_Test_System">         SELECT Score         FROM dbo.[de8e2958-dffe-4db0-8b01-2447294db854]         WHERE Student_AD_Username = '#students.AD_Username#' AND Test_ID = '#tests.Test_ID#'         ORDER BY Test_ID, Student_Lname, Student_Fname       </cfquery>     </cfloop>     <cfloop query="scores">       <td>#scores.Score#%</td>        </cfloop> </cfoutput></tr> </table>

    This topic has been closed for replies.

    1 reply

    Inspiring
    July 30, 2009

    My suggestion is to write a single query that gets what you want from all three tables.  If you don't know how, I've heard good things about the book, "Teach Yourself SQL in 10 Minutes" by Ben Forta.