HTML Table with columns sorted from top to bottom and left to right.
Fri September 11 '09 4:09 PM Category: ColdFusion , HTMLSomething happens occasionally when I am writing some pretty simple code and I realize that what I am trying to do is not as simple as I initially imagined.
I wanted to create a table with an alphabetical list where the list begins top to bottom in the first column and then continues at the top of the next column, based on how many columns I have specified. The example I am using had 4 columns and it used a query to output the results in each <td>. This is usefukl when outputting an alphabetical list using a bunch of horizontal space.
<table >
<tr>
<cfset currentrow = 0>
<cfset cols = 4>
<cfset rows = ceiling(qry.recordcount/cols)>
<cfloop from="1" to="#rows#" index="row">
<cfloop from="1" to="#cols#" index="col">
<cfif col eq 1>
<cfset currentrow = row>
<cfelse>
<cfset currentrow = currentrow+rows>
</cfif>
<cfif currentrow gt qry.recordcount>
<td> </td>
<cfelse>
<td>
#qry.val[currentrow]#
</td>
</cfif>
</cfloop>
<tr>
</cfloop>
</tr>
</table>
So what is happening is first I start the table and first row, then set the currentrow default value. The colums can be set to whatever amount and the amount of rows will be calculated to a whole number that can then be used as the row counter. Begin the outside loop for rows, the inside loop for columns. If the column is the first one, reset the counter the the row number, then for each additional row take the current row, add the total rows and continue until the row is reset back at the first column. Pretty simple once I finally realized how it was going to work. My co-worker said he can accomplish the same thing in an output query loop. I'm sure he could but not with as few lines as mine. Hahaha.
Loading...
Recent Comments