HOME | FERGUSON Digital Blog

Entries for month: September 2011

Using MOD 4 With CFOUTPUT (redux)

ColdFusion · By Michael Ferguson No 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.

Using MOD 4 With CFOUTPUT

ColdFusion · By Michael Ferguson 2 Comments »

Here is an example of how you can use MOD (Modulo) with CFOUTPUT to build a table to display four pairs of repeating data from a query structure. Using this method will build the table with the information ITEM and TOTAL repeating four times across each row. In the event that the total number of records in the query are not divisible by four, the table cells will end early.

<table border="0" cellpadding="0" cellspacing="0">
   <tr>
      <th>ITEM</th>
      <th>TOTAL</th>
   </tr>
   <cfoutput query="GetInfo">
      <cfif GetInfo.CurrentRow MOD 4 EQ 1 OR GetInfo.CurrentRow EQ 1><tr></cfif>
         <td width="20%">#GetInfo.Item#</td>
         <td width="5%">#GetInfo.Total#</td>
      <cfif GetInfo.CurrentRow MOD 4 EQ 0 OR GetInfo.RecordCount></tr></cfif>
   </cfoutput>
</table>

This example may need to be tweaked for your purpose but it may help you avoid developing an unnecessarily complicated solution for a simple data display puzzle. Using MOD works because when the CurrentRow is divided by 4 evaluates as 0 when progressing through the query. First time through the loop the CurrentRow is 1 which is not divisible by 4 so it progresses to the next CurrentRow. This keeps going until it reaches a CurrentRow that is divisible by 4 then ends the row and starts a new one.

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

© Copyright 1997-2024, All Rights Reserved Coldfusion and MS SQL2008
Powered by Mango Blog.   Design by FERGUSON Digital