Search code examples
coldfusionstructcfloop

Looping over a Cfquery or a Struct?


I have a query which retrieves Some data. I want to display that data considering some conditions in different div tags. Now my question is, I am doing this by looping the query once and getting the data in three different structs and using these structs while displaying. Is this a good approach or looping through the query everytime in each div to check the condition is the rirht approach ?

     <tr >
<td >
  features:
 </td>
 <td >
    <cfloop query="getAttributes">
      <cfif getAttributes.type_id EQ 1>
        #getAttributes.seat#<br>
      </cfif>
    </cfloop>
 </td>
</tr>
<tr>
 <td >
  Disclosures:
 </td>
 <td >
    <cfloop query="getAttributes">
   <cfif getAttributes.type_id EQ 2>
          #getTicketAttributes.seat#<br>
   </cfif>
  </cfloop>
  </td>
 </tr> 

Or can i use the below approach

seatStruct 
disclosureStruct 
<cfloop query="getAttributes">  
<cfif getAttributes.type_id EQ 1> 
Insert seatStruct 
<cfelseif getAttributes.type_id EQ 2> 
insert disclosureStruct 
</cfif> 
Now use these structs to display

Solution

  • I think you'll have to edit your question a little bit, put some example.

    Less looping is always the best approach :) Less conversion if not necessary is best approach :)

    If your data is in one query, than there's no need to loop more than once, I guess...