Search code examples
coldfusioncfloop

table showing more than once, How to change?


I have this code that shows a table:

<cfloop  query="GetResults2">
<cfif GetResults2.dept_id eq aFieldValue>     
<table class="table1">
<th>Name</th><th>Positive Comment</th><th>Negative Comment</th>
<tr>
     <td nowrap="nowrap">#emp_namefirst# #Left(emp_namelast, 1)#  </td>
    <td>#Replace(commentpositive, emp_namefirst, "<B>" & emp_namefirst & "</B>")#</td>
    <td>#Replace(commentnegative, emp_namefirst, "<B>" & emp_namefirst & "</B>")#</td>
    </tr>


    </table>
</cfif>
</cfloop>

Right now it does get the correct results but its loopping every time and creating a new table for every row. How can I get it that it will only show one table? I have try moving the

<cfloop query="GetResults2"> inside the table but that doesnt solve the problem. Any suggestions on how to solve it?


Solution

  • This will create one table with a row for each result in the query. The loop needs to be nested within the table and after the header rows.

    <table class="table1">
      <th>Name</th>
      <th>Positive Comment</th>
      <th>Negative Comment</th>
      <cfloop  query="GetResults2">
        <cfif GetResults2.dept_id eq aFieldValue>     
          <tr>
            <td nowrap="nowrap">#emp_namefirst# #Left(emp_namelast, 1)#  </td>
            <td>#Replace(commentpositive, emp_namefirst, "<B>" & emp_namefirst & "</B>")#</td>
            <td>#Replace(commentnegative, emp_namefirst, "<B>" & emp_namefirst & "</B>")#</td>
          </tr>
        </cfif>
      </cfloop>    
    </table>