HOME | FERGUSON Digital Blog

Using MOD 4 With CFOUTPUT (redux)

ColdFusion Add comments

Yesterday I posted about how you could use the "MOD (Modulo) with CFOUTPUT to build a table to display four pairs of repeating data from a query structure" and thanks to a response by Michael Zock, I realized that my example was not proofread so here is a redux example of Modulo driven column display from a query.

<!---USE NORTHWIND DATABASE TO BUILD QUERY--->
<cfquery name="GetInfo" datasource="Northwind">
   SELECT OrderID, ShipName
   FROM Orders
</cfquery>

<table border="1" cellpadding="0" cellspacing="0">
   <tr>
      <!---LOOP FOUR TIMES TO BUILD REPEATING HEADER--->
      <cfloop index="LoopIndex" from="1" to="4">
         <th>Order ID</th>
         <th>Ship Name</th>
      </cfloop>
   </tr>
   <!---OUTPUT QUERY--->
   <cfoutput query="GetInfo">
      <!---USE MODULO 4 TO DETERMINE IF CURRENT RECORD DIVISIBLE BY FOUR FOR TR--->
      <cfif GetInfo.CurrentRow MOD 4 EQ 1 OR GetInfo.CurrentRow EQ 1><tr></cfif>
         <td width="5%">#GetInfo.OrderID#</td>
         <td width="20%">#GetInfo.ShipName#</td>
      <!---USE MODULO 4 AND CHECK IF END OF QUERY FOR CLOSING TR OF EACH ROW--->
      <cfif GetInfo.CurrentRow MOD 4 EQ 0 OR GetInfo.RecordCount EQ GetInfo.CurrentRow>
         <!---CHECK IF LAST ROW HAS LESS THAN 4 RESULTS TO PAD TABLE--->
         <cfloop index="LoopIndex" from="1" to="#GetInfo.CurrentRow MOD 4#">
            <td width="5%"> </td>
            <td width="20%"> </td>
         </cfloop>
         </tr>
      </cfif>
   </cfoutput>
</table>
<br /><br />
<!---EXAMPLE REPEATED WITH QUERY MAXROWS 16 (4x4) TO PROVE CLOSING TR AND QUERY END CHECK--->
<table border="1" cellpadding="0" cellspacing="0">
   <tr>
      <cfloop index="LoopIndex" from="1" to="4">
         <th>Order ID</th>
         <th>Ship Name</th>
      </cfloop>
   </tr>
   <cfoutput query="GetInfo" maxrows="16">
      <cfif GetInfo.CurrentRow MOD 4 EQ 1 OR GetInfo.CurrentRow EQ 1><tr></cfif>
         <td width="5%">#GetInfo.OrderID#</td>
         <td width="20%">#GetInfo.ShipName#</td>
      <cfif GetInfo.CurrentRow MOD 4 EQ 0 OR GetInfo.RecordCount EQ GetInfo.CurrentRow>
         <cfloop index="LoopIndex" from="1" to="#GetInfo.CurrentRow MOD 4#">
            <td width="5%"> </td>
            <td width="20%"> </td>
         </cfloop>
         </tr>
      </cfif>
   </cfoutput>
</table>

I hope this more complete example better illustrates a use for the MOD (Modulo) than just alternating row colors for table rows!

If you find this post useful please leave a comment and let me know how you used the information.

Download zipped example HERE.

0 responses to “Using MOD 4 With CFOUTPUT (redux)”

Leave a Reply




© Copyright 1997-2024, All Rights Reserved Coldfusion and MS SQL2008
Powered by Mango Blog.   Design by FERGUSON Digital
o:Event name="beforeHtmlBodyEnd" />