Skip to main content
September 17, 2008
Question

Split up cfquery results

  • September 17, 2008
  • 2 replies
  • 1130 views
Hello. I am querying my database and usually the resultset contains about 100 records. I want to split up my results dynamically into three columns instead of just listing them out like I currently do. I am not sure how to do this. This is pretty generic but is the format I'm currently using...

<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td>

<!--- Get and return related waters --->
<cfquery name="query" datasource="myds">
SELECT id FROM my_db WHERE state = '#state#'
</cfquery>

<cfoutput query="query">
#id#<br>
</cfoutput>

</td>
</tr>
</table>


I want to have it so when the result row is 40, create a new column and display the next 40 results, and so on.

Can someone please help?

Thanks!
    This topic has been closed for replies.

    2 replies

    September 17, 2008
    Works great! Thank you.
    Inspiring
    September 17, 2008
    do something like:

    <!--- Get and return related waters --->
    <cfquery name="query" datasource="myds">
    SELECT id FROM my_db WHERE state = '#state#'
    </cfquery>

    <cfset maxRow = 40> <!--- set the maximum row of table to be displayed --->

    <!--- Get the number of columns(noOfCol) to accomodate all data --->
    <cfset noOfCol = query.recordcount\maxRow>
    <cfif query.recordcount Mod maxRow neq 0>
    <cfset noOfCol = noOfCol + 1>
    </cfif>

    <table width="100%" cellpadding="0" cellspacing="0" border="1">
    <cfloop from="1" to="#maxRow#" index="nRow">
    <cfset nDataRow = nRow> <!--- initialize to current row --->
    <tr>
    <cfloop from="1" to="#noOfCol#" index="i">
    <td><cfoutput>#query.id[nDataRow]#</cfoutput></td>
    <cfset nDataRow = nDataRow + maxRow>
    </cfloop>
    </tr>
    </cfloop>
    </table>