Horizontal Loop
Many of you may have had a time when you have wanted to output from a query in a grid format.
This script will enable you to loop through the results horizontally, across the screen
Here is a demo of the result
The first thing that we need to do it to query a database
SELECT *
FROM table
</cfquery>
Now that we have the data we can decide how many columns we would like across the screen in this example i will have 5. I will set a variable to that after every 5th result i will start a new line.
We will now create a table to output the data into. within this table we will repeat the <td> tag and after every 5th result output </tr><tr> to end the line and start a new one. we will then increase the count for the end of the next row.
<tr>
<cfoutput query="items">
<td#items.name#</td>
<cfif CurrentRow eq multiple>
</tr><tr>
<cfset multiple = multiple + 5>
</cfif>
</cfoutput>
</tr>
</table>
Here is the full script for you
SELECT *
FROM table
</cfquery>
<cfset multiple = 5>
<table width="300" border="0" cellspacing="1" cellpadding="1">
<tr>
<cfoutput query="items">
<td>#items.name#</td>
<cfif CurrentRow eq multiple>
</tr><tr>
<cfset multiple = multiple + 5>
</cfif>
</cfoutput>
</tr>
</table>
Enjoy
