Example 10.4: Populating an Array with Query Data

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 are going to query a database to get all employee names and their extension numbers. We will then create an array to hold this information and will store this array in the Application scope.

Next we will use another template to recall and display the company phone list. Because this example relies on the use of the Application scope, make sure your Examples\Step10 folder contains a copy of the Application.cfm file created in Step 8, "ColdFusion Application Framework."

  1. Open your text editor and type the code in Listing 10.5 or open the Array3.cfm file from the CompletedFiles\Examples\Step10 folder.

    Listing 10.5 Array3.cfm
     <!--- File:             Array2.cfm  Description:      Demo using queries to populate an array  Author:  Created:  --->  <HTML>    <HEAD>        <TITLE>Array 3</TITLE>    </HEAD>    <BODY>      <!--- lock the variable and create the array --->      <CFLOCK TIMEOUT="10" THROWONTIMEOUT="No" TYPE="EXCLUSIVE" SCOPE="APPLICATION">              <CFSET APPLICATION.aPhoneList=ArrayNew(2)>        </CFLOCK>        <!--- retrieve database information. --->        <CFQUERY NAME="qPhoneList" DATASOURCE="#MyAppDSN#">              SELECT      FirstName, LastName, Extension              FROM        Employees              ORDER BY    LastName        </CFQUERY>        <!--- populate the array--->        <CFLOCK TIMEOUT="10" THROWONTIMEOUT="No" TYPE="EXCLUSIVE" SCOPE="APPLICATION">              <CFLOOP QUERY="qPhoneList">                     <CFSET APPLICATION.aPhoneList[CurrentRow][1]=qPhoneList.LastName>                     <CFSET APPLICATION.aPhoneList[CurrentRow][2]=qPhoneList.FirstName>                     <CFSET APPLICATION.aPhoneList[CurrentRow][3]=qPhoneList.Extension>              </CFLOOP>        </CFLOCK>        <P><A HREF="PhoneList.cfm">Display Phone List</A></P>        <CFLOCK TIMEOUT="10" THROWONTIMEOUT="No" TYPE="READONLY" SCOPE="APPLICATION">              <CFDUMP VAR="#APPLICATION.aPhoneList#">        </CFLOCK>    </BODY>  </HTML>
  2. Save the file as Array3.cfm in your Example\Step10 folder.

  3. Browse to the Array3.cfm file. You should see something similar to Figure 10.11.

    Figure 10.11. The Array3.cfm browser display.

    graphics/10fig11.gif

    In this template, we create an Application scope variable to hold our two-dimensional array. We then query the database to retrieve all of our employees' phone numbers. Then, using a Query loop, we populate the array with the query information and provide a link to the phone list display.

  4. Open your text editor and type the code in Listing 10.6 or open the PhoneList.cfm file from the CompletedFiles\Examples\Step10 folder.

    Listing 10.6 PhoneList.cfm
     <!--- File:             PhoneList.cfm  Description:      Demo use of application scope array  Author:  Created:  --->  <HTML>    <HEAD>        <TITLE>Company Phone List</TITLE>    </HEAD>    <BODY>        <H2>My Company Phone List</H2>        <!--- create a table --->        <TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0">              <TR BGCOLOR="#FFFFCC">                    <TH>Last Name</TH>                    <TH>First Name</TH>                    <TH>Extension</TH>              </TR>              <!--- outer loop, value of x for each row --->              <CFLOCK TIMEOUT="10" THROWONTIMEOUT="No" TYPE="READONLY" SCOPE="APPLICATION">              <CFLOOP INDEX="x" FROM="1" TO="#ArrayLen(APPLICATION.aPhoneList)#">              <TR>                <!--- inner loop, value of y for each column --->                  <CFLOOP INDEX="y" FROM="1" TO="#ArrayLen(APPLICATION.aPhoneList[x])#">                     <TD>                     <CFOUTPUT>#APPLICATION.aPhoneList[x][y]#</CFOUTPUT>                     </TD>                  </CFLOOP>               </TR>               </CFLOOP>             </CFLOCK>    </TABLE>    </BODY>  </HTML>

  5. Save the file as PhoneList.cfm in your Examples\Step10 folder.

  6. Browse to the PhoneList.cfm file. You should see something similar to Figure 10.12.

    Figure 10.12. The PhoneList.cfm browser display.

    graphics/10fig12.gif

Take care to lock the Application scope variable every time it is accessed.


    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