Example 10.1: Creating, Passing, and Displaying a List

Team-Fly    

ColdFusion® MX: From Static to Dynamic in 10 Steps
By Barry Moore
Table of Contents
Step 10.  Using Lists, Arrays, and Structures


In this example, we will create the list on one page and pass it to another using a URL variable. The second page will use this list to build a drop-down menu.

  1. Open your text editor and type the code in Listing 10.1 or you can open the List1.cfm file from the CompletedFiles\Examples\Step10 folder.

    Listing 10.1 List1.cfm
     <!--- File:             List1.cfm  Description:      Demo the use of list variable  Author:  Created:  --->  <!--- create the list --->  <CFSET lDepts="Marketing,Operations,Sales,Technology">  <HTML>    <HEAD>        <TITLE>Create The List</TITLE>    </HEAD>    <BODY>        <P>We have created a list with four elements.        Follow the link to pass this list to another page.</P>        <P>The List looks like this:</P>        <!--- output the list --->        <P><CFOUTPUT>#lDepts#</CFOUTPUT></P>        <P><CFOUTPUT><A HREF="List2.cfm?list=#lDepts#">Next Page</A></CFOUTPUT></P>    </BODY>  </HTML>

  2. Save the file as List1.cfm in your Examples\Step10 folder.

  3. Now we will create the page with the drop-down menu. Open your text editor and type the code in Listing 10.2 or you can open the List2.cfm file from the CompletedFiles\Examples\Step10 folder.

    Listing 10.2 List2.cfm

    [View full width]

     <!--- File:             List2.cfm  Description:      Demo the use of list variable  Author:  Created:  --->  <HTML>    <HEAD>        <TITLE>Create The List</TITLE>    </HEAD>    <BODY>        <P>Use the list passed for the previous page to create a drop- down menu.</P>        <!--- loop through the list to create the menu --->        <FORM>              <SELECT NAME="DeptMenu">                    <CFLOOP INDEX="x" LIST="#URL.List#">                      <CFOUTPUT>                          <OPTION VALUE="#x#" SELECTED>#x#</OPTION>                      </CFOUTPUT>                    </CFLOOP>              </SELECT>        </FORM>        <P>If you store the list in the Server, Application, or Session scope,<BR> you will  graphics/ccc.gifbe able to access the list from any page without having to manually<BR> pass the variable  graphics/ccc.giffrom page to page.</P>    </BODY>  </HTML>
  4. Save the file as List2.cfm in your Examples\Step10 folder.

  5. Browse to the Examples\Step10\List1.cfm page. You should see something similar to Figure 10.1.

    Figure 10.1. The List1.cfm browser display.

    graphics/10fig01.gif

  6. Follow the link to List2.cfm. You should see something similar to Figure 10.2.

    Figure 10.2. The List2.cfm browser display.

    graphics/10fig02.gif

We have used a URL variable to pass this list value. It is more common to store list values in one of the shared memory scopes. This way, the list variable is available to templates throughout the application without having to explicitly pass the list variable every time.


    Team-Fly    
    Top
     



    ColdFusion MX. From Static to Dynamic in 10 Steps
    ColdFusion MX: From Static to Dynamic in 10 Steps
    ISBN: 0735712964
    EAN: 2147483647
    Year: 2002
    Pages: 140
    Authors: Barry Moore

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net