SfC Home > ColdFusion >
Explanation of how to populate a complex matrix table using the Macromedia ColdFusion markup language. Also refer to database, SQL, query, format, CFLOOP, loop, maximum rows, Ron Kurtus, School for Champions. Copyright © Restrictions
Populating a Matrix Table with ColdFusion
by Ron Kurtus (26 December 2002)
Taking information from a database and linearly populating the rows of a table is relatively simple in Macromedia ColdFusion. It is more difficult to populate a table that is a matrix, with both columns and rows. The method is similar to populating a simple table, except that you, must define the number of rows and then loop through the matrix.
Questions you may have include:
- How is a simple table populated?
- How do you populate a matrix?
- What does the output look like?
This lesson will answer those questions. There is a mini-quiz near the end of the lesson.
Simple table
The method to simply populate the rows of a table in a given sequence is:
<CFQUERY NAME="list" DATASOURCE="xyz" DBTYPE="ODBC">
select * from table_name
order by seq_no
</CFQUERY>
<TABLE>
<CFOUTPUT QUERY="list">
<TR> <TD>#seq_no#: #name#</TD></TR>
</CFOUTPUT>
</TABLE>
This is pretty straightforward.
Populate matrix
To populate a matrix table of 3 columns by a number of rows determined by the amount of data you have, you first make the same query as in the simple case. The data in this table is ordered by a sequence number. You could also order the data by ID value.
<CFQUERY NAME="list" DATASOURCE="xyz" DBTYPE="ODBC">
select * from table_name
order by seq_no
</CFQUERY>
Define number of rows
Find the number of records in your table. Then set the maximum rows as the integer of the number of records + 2, divided by 3. If the number of columns was 5, the maximum rows would be the integer of the number of records + 4, divided by 5.
<CFSET max_seq = #set_count.recordcount#>
<CFSET max_rows = INT((#max_seq# +2)/3)>
Loop through matrix
First set a variable x = 0. Loop through the number of rows and then the columns.
<CFSET x = 0>
<TABLE>
<CFLOOP INDEX="seq_no" FROM="1" TO="#max_rows#">
<TR>
<CFLOOP INDEX="sequence" FROM="1"
TO="3">
<CFSET x = x + 1>
<CFQUERY NAME="populate"
DATASOURCE="xyz">
SELECT * FROM logo_info
WHERE sequence = #x#
ORDER BY sequence
</CFQUERY>
<TD>
<CFOUTPUT QUERY="populate"> #seq_no#: #name#</CFOUTPUT>
</TD>
</CFLOOP>
</TR>
</CFLOOP>
</TABLE>
Output
The output table would look something like:
1: Joe |
2: Harry |
3: Betty |
4: Mary |
5: Bill |
6: Jose |
7: Sue |
This is an example where the maximum rows is the integer of (7 + 2)/3 = 3
Summary
You can populate a table that is a matrix, with both columns and rows, by defining the maximum number of rows, looping through the rows, plus looping through the columns for each row.
Maintain a healthy attitude toward your work
Resources
The following resources provide information on this subject.
Websites
Books
Mini-quiz to check your understanding
1. What is ColdFusion Studio?
2. What is needed to display ColdFusion pages on the Web?
3. What is a major application of ColdFusion?
If you got all three correct, you are on your way to becoming a Champion in ColdFusion Development. If you had problems, you had better look over the material again.
What do you think?
Do you have any questions, comments, or opinions on this subject? If so, send an email with your feedback. We will try to get back to you as soon as possible.
Share link
Feel free to establish a link from your website to pages in this site.
Or use our form to send this link to yourself or a friend.
Students and researchers
The Web address of this page is
www.school-for-champions.com/coldfusion/populate_matrix.htm. Please
include it as a reference in your report, document, or thesis.
Where can you go from here?
Populating a Matrix Table with ColdFusion
