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

Archives By Subject

Advertising (3) [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]
JavaScript (4) [RSS]
PHP (1) [RSS]
Useful Tools (3) [RSS]

Recent Comments

Coldfusion to PHP?
psikoteknik said: Very nice blog.Thanks for the valuable contributions to this site. Have good works. Thanks for your... [More]

Coldfusion Hosting From £2.50
Jonny Shaw said: Yes sandboxing is enabled, and no tags are disabled. I am aware of the issure with IE7, stupid IE an... [More]

Coldfusion Hosting From £2.50
Gary F said: Sounds good. Do you use sandboxing and are any tags disabled? Your jassoft.co.uk home page doesn't r... [More]

Coldfusion to PHP?
Rob Wilkerson said: You won't have any problems as long as you keep your mind open. I made the same move about 2 years a... [More]

Coldfusion to PHP?
Jonny Shaw said: The framework i will be using is Zend, Has anyone used this? [More]

Recent Entries

Coldfusion Hosting From £2.50
cf8.co.uk For Sale
Coldfusion Blog | Coldfusion Hosting | About Me