Dynamically Outputting Query Data 'X' Columns Across
Dynamically outputting query data in "X number of columns across" is no new thing, but since I found myself having to do that very thing this morning, I thought I'd share this sweet little snippet for the benefit of everyone (but mostly for my own snippet collection since I seem to tend to "forget what I once new"... lol).
In this example I'm going to output a series of short questions using the following query result:

WIth the final html and result looking like this:

Without further adieux, your snippet:
In this example I'm going to output a series of short questions using the following query result:

WIth the final html and result looking like this:

<table width="100%">
<tr>
<td>Dead loved ones present</td>
<td>Animal(s) as prominent player(s)</td>
<td>"Significant Other" present</td>
</tr>
<tr>
<td>Familiar Setting</td>
<td>Unfamiliar Setting</td>
<td>Awoke Emotional</td>
</tr>
<tr>
<td>Dreamed you were Awake</td>
<td> </td>
<td> </td>
</tr>
</table>
<tr>
<td>Dead loved ones present</td>
<td>Animal(s) as prominent player(s)</td>
<td>"Significant Other" present</td>
</tr>
<tr>
<td>Familiar Setting</td>
<td>Unfamiliar Setting</td>
<td>Awoke Emotional</td>
</tr>
<tr>
<td>Dreamed you were Awake</td>
<td> </td>
<td> </td>
</tr>
</table>
generated html
Without further adieux, your snippet:
<cfset numColumns = 3 /><!--- set the number of columns to output --->
<!--- determine number of total rows to output --->
<cfif qryQuestions.recordcount mod numColumns eq 0>
<cfset numrows = qryQuestions.recordcount/numColumns>
<cfelse>
<cfset numrows = int(qryQuestions.recordcount/numColumns) + 1>
</cfif>
<!--- output the table! --->
<table width="100%">
<cfloop from="1" to="#numrows#" index="i">
<tr>
<!--- create columns for this row 'i' --->
<cfloop from="1" to="#numColumns#" index="j">
<!--- calculate which record we're outputting --->
<cfset qryRow = ((i-1)*numColumns)+j >
<!--- ...as long as wer're not trying to access a query record beyond our 'recordcount' value... --->
<cfif qryRow lte qryQuestions.recordcount>
<td><cfoutput>#qryQuestions["question"][qryRow]#</cfoutput></td>
<cfelse><!--- output an empty 'filler' td... --->
<td> </td>
</cfif>
</cfloop>
</tr>
</cfloop>
</table>
<!--- determine number of total rows to output --->
<cfif qryQuestions.recordcount mod numColumns eq 0>
<cfset numrows = qryQuestions.recordcount/numColumns>
<cfelse>
<cfset numrows = int(qryQuestions.recordcount/numColumns) + 1>
</cfif>
<!--- output the table! --->
<table width="100%">
<cfloop from="1" to="#numrows#" index="i">
<tr>
<!--- create columns for this row 'i' --->
<cfloop from="1" to="#numColumns#" index="j">
<!--- calculate which record we're outputting --->
<cfset qryRow = ((i-1)*numColumns)+j >
<!--- ...as long as wer're not trying to access a query record beyond our 'recordcount' value... --->
<cfif qryRow lte qryQuestions.recordcount>
<td><cfoutput>#qryQuestions["question"][qryRow]#</cfoutput></td>
<cfelse><!--- output an empty 'filler' td... --->
<td> </td>
</cfif>
</cfloop>
</tr>
</cfloop>
</table>
Subscription Options
You are not logged in, so your subscription status for this entry is unknown. You can login or register here.
No comments found.

