SfC Home > ColdFusion >
Explanation of how to list a defined number of items at a time from a ColdFusion query. Also refer to database, table, functions, if, then, CFIF, CFSET, session manager, Ron Kurtus, School for Champions. Copyright © Restrictions
List 10 at a Time from a ColdFusion Query
by Ron Kurtus (revised 3 July 2001)
You can list items from a database table with a common query. Even if you filter the results, you may get a long list of items. There is a method to list a select number of items at a time.
Questions you may have include:
- What is a simple way of doing it?
- What code is required?
- How does it work?
This lesson will answer those questions. There is a mini-quiz near the end of the lesson.
First 10 items
Suppose you make a simple query.
<CFQUERY NAME="list_items" DATASOURCE="db_name">
SELECT item FROM my_tables
ORDER BY itemID
</CFQUERY>
To get the first 10 items, you set boundaries in your output.
<CFOUTPUT QUERY="list_items" STARTROW="1" MAXROWS="10">
<P>#item#</P>
</CFOUTPUT>
Repeated method
To list the first 10 items and then be able to click on a link to repeatedly show the next 10, you could use the following code:
<CFAPPLICATION NAME="Next10" SESSIONMANAGEMENT="Yes">
<CFQUERY NAME="list_items" DATASOURCE="db_name">
SELECT item FROM my_tables
ORDER BY itemID
</CFQUERY><CFIF IsDefined("url.next")>
<CFSET session.start = #session.start# + 10>
<P>List of next 10 items</P>
<CFOUTPUT QUERY="list_items" STARTROW="#session.start#" MAXROWS="10">
<P>#item#</P>
</CFOUTPUT><P><A HREF="this_page.cfm?next=10">Next 10</A></P>
<CFELSE>
<P>List of first 10 items</P>
<CFSET start = 1>
<CFSET session.start = #start#>
<CFOUTPUT QUERY="list_items" STARTROW="#session.start#" MAXROWS="10">
<P>#item#</P>
</CFOUTPUT><P><A HREF="this_page.cfm?next=10">Next 10</A></P>
</CFIF>
Explanation
You really want the whole application to be on one page. When you open the page, url.next has not been defined, so you start at the <CFELSE> term.
Session management is used so that the variables that you set will be carried forward, when you click the link to go to the next view of this page.
The expression in the link: ?next=10 will send url.next to the <CFIF> tag. "Next" could be set to equal anything. I used 10 just to indicate a list of 10 items.
In the <CFIF> section, the STARTROW is now the previous one plus 10.
To list a items other than 10 at a time, change the value of X in MAXROWS="X" and <CFSET session.start = #session.start# + X>.
Summary
This is an easy way to list the output of your query 10 items at a time. You can adjust it to list any number you wish.
Programming rocks
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/next10.htm.
Please include it as a reference in your report, document, or thesis.
Where can you go from here?
List 10 at a Time from a ColdFusion Query
