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

<cfquery datasource="loop" name="items">
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.

<cfset multiple = 5>

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.

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

Here is the full script for you

<cfquery datasource="loop" name="items">
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

Related Blog Entries

Comments
MTM's Gravatar Thank you....
# Posted By MTM | 30/04/09 15:33

Archives By Subject

Advertising (4) [RSS]
Blog Design (1) [RSS]
CFProject Scripts (2) [RSS]
Coldfusion Charts (3) [RSS]
Coldfusion Functions (5) [RSS]
Coldfusion Overview (1) [RSS]
Coldfusion Tutorials (17) [RSS]
For Sale (2) [RSS]
Image Manipulation (1) [RSS]
Java (1) [RSS]
JavaScript (4) [RSS]
PHP (1) [RSS]
Railo (2) [RSS]
SQL (1) [RSS]
Useful Tools (3) [RSS]

Recent Comments

Hashing Passwords
Richard Lord said: You really need to salt the passwords before hashing them, otherwise you're exposed to a rainbow-tab... [More]

Using Railo and Resin with domain aliases
Jonny Shaw said: Thanks for that. I am already using <host regexp="(.+)"> <host-name>${ho... [More]

Using Railo and Resin with domain aliases
Gert Franz said: Hi Jonny, it's even easier if you do it this way: <host id="allmead.com" root-dir... [More]

BlogCFC on Railo
Jonny Shaw said: Thanks for the advice ray. I was surprised at how easily BlogCFC went accross to to railo. The onl... [More]

BlogCFC on Railo
Gert Franz said: Congratulations to your new websites. I just wanted to add two things: CFIMAGE and CFCHART are suppo... [More]

Recent Entries

No recent entries.
ColdFusion Blog | ColdFusion Hosting | ColdFusion Q & A