Server Time:
Thursday May 15 2008 11:03 PM  
Your Time:
  
HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

Dynamically Generating HTML Table Columns & Rows
by: James Harvey
Email this tutorial to a friend Display Printer Friendly Format
[Download in PDF Format] [Download in FlashPaper Format]

Dynamically Generating HTML Table Columns & Rows
by: James Harvey
 


I've written this tutorial, because I had a need to do exactly what the title suggests. I needed to dynamically generate a table with Columns and Rows, based off of a CFQUERY results. Well, this is the fruit of my labors, and I hope it assists others as well.

Introduction:
In this example you are treating your recordset as though it was a structure. The first loop will give you your rows and the nested loop will give you your cells.
To change the number of columns in the table you will modify the variable NumberOfColumns.

The Script:
<cfquery name="parks" datasource="cfdocexamples">
select top 7 parkName, parkType, city, state from parks order by parkName
</cfquery>
<cfset NumberOfColumns = 5>
<cfset idx2Counter = 1>
<cfset addTo = parks.RecordCount MOD NumberOfColumns>
<!---\\ If you have a remainder you need to add 1 more row. //--->

<cfif addTo GT 0>
<cfset addTo = 1>
</cfif>

<table border="1">
<cfloop from="1" to="#(parks.RecordCount/NumberOfColumns) + addTo#" index="idx">
<tr>
<cfloop from="1" to="#NumberOfColumns#" index="idx2">
<td valign="top">
<cfoutput>
<!---\\This is the content of the Cell Pulled from the database query above//--->
<cfif Len(parks.parkName[idx2Counter])>
#parks.parkName[idx2Counter]#<br>
#parks.parkType[idx2Counter]#<br>
#parks.city[idx2Counter]#, #parks.state[idx2Counter]#
<cfelse>
&nbsp;
</cfif>
<!---\\Now We end out content output//--->
</cfoutput>
</td>
<cfset idx2Counter = idx2Counter + 1>
</cfloop>
</tr>
</cfloop>
</table>

This will now generate a table with 5 columns and X number of rows.
Enjoy!

-James


Date added: Thu. April 3, 2008
Posted by: James Harvey | Views: 430 | Tested Platforms: CFMX,CFMX7,CFMX8 | Difficulty: Beginner
Categories Listed: Code Snipplets Databases Working w/Data

HostMySite.Com is sponsoring this tutorial, please visit their site today!
This tutorial is sponsored by HostMySite.Com - ColdFusion Hosting

This author's other tutorials:
Displaying Tag Restrictions on ColdFusion Server
This Tutorial will show you how to display tag restricitons with Sandbox Security enabled. - Date added: Thu. March 6, 2008
Please rate this tutorial:
5 Stars 4 Stars 3 Stars 2 Stars 1 Stars
Post a new comment on this tutorial
post a new comment on this particular tutorial
Your Name:
Your Email:
Comment Title:
Comments:
Key Phrase:
 
Skyscrapper Banner Advertisement
ProWorkFlow.Com

You are 1 of 63 active sessions! | Privacy | Company
Copyright © 2002 EasyCFM.Com, LLC. (Easy ColdFusion Tutorials) All Rights Reserved
All other trademarks and copyrights are the property of their respective holders.
ColdFusion Hosting ColdFusion Hosting
ADD TO:
Blink
Del.icio.us
Digg
Furl
Google
Simpy
Spurl
Y! MyWeb