HOME | FERGUSON Digital Blog

Using MOD 4 With CFOUTPUT

ColdFusion Add 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.

2 responses to “Using MOD 4 With CFOUTPUT”

  1. Michael Zock on Thanks for sharing. This should help people to better understand the practical uses of modulo operations.

    The recordCount check in the lower <cfif/> should be a separate check (along the lines of "GetInfo.RecordCount MOD 4") after the loop has finished. The way it's worded right now the second part of the if clause would fire during every loop iteration.
  2. Michael Ferguson on Michael Zock,

    After reading your response, I realized my example contained a error that needs correcting to make it more accurately represent the intent of the article. I'm guilty of posting before proofreading on this one!

    Michael Ferguson

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" />