Structures

Team-Fly    

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


Before we move on to the final makeover exercise, let's have a quick introduction to structures. Structures are another complex data construct very similar to an array. As a matter of fact, they are sometimes known as associative arrays.

Structures are very similar in organization and use to arrays, but instead of containing numerical references to element locations, they contain name/value pairs such as FirstName=Emma. The code in Listing 10.7 shows how we could retrieve all details for a particular employee and store them in a single structure variable.

Listing 10.7 Employee Structure
 <!--- File:             Structure1.cfm  Description:      Demo using queries and structures  Author:  Created:  --->  <HTML>    <HEAD>        <TITLE>Structure 1</TITLE>    </HEAD>    <BODY>        <!--- create the structure --->        <CFSET stEmployeeDetails=StructNew()>          <!--- retrieve database information. --->          <CFQUERY NAME="qEmployee" DATASOURCE="#MyAppDSN#">                SELECT      *                FROM        Employees                WHERE       EmployeeID = 4          </CFQUERY>          <!--- populate the structure --->          <CFSET stEmployeeDetails.EmployeeID=qEmployee.EmployeeID>          <CFSET stEmployeeDetails.FirstName=qEmployee.FirstName>          <CFSET stEmployeeDetails.LastName=qEmployee.LastName>          <CFSET stEmployeeDetails.DepartmentID=qEmployee.DepartmentID>          <CFSET stEmployeeDetails.Title=qEmployee.Title>          <CFSET stEmployeeDetails.Email=qEmployee.Email>          <CFSET stEmployeeDetails.Extension=qEmployee.Extension>          <CFSET stEmployeeDetails.DateHired=qEmployee.DateHired>          <!--- dump the structure --->          <CFDUMP VAR="#stEmployeeDetails#">      </BODY>   </HTML>

This code creates a structure using the StructNew() function. Note that we are using "st" as a variable prefix to denote that the EmployeeDetails variable is a structure (see the "Whats In a Name?" sidebar in Step 2). We then retrieve a particular employee's details using a query. Then we use a series of <CFSET> statements to add the details to the structure. Finally, we use the <CFDUMP> tag to display the contents of the structure. Figure 10.13 shows the result of this code.

Figure 10.13. The Structure1.cfm browser display.

graphics/10fig13.gif

For more information on structure functions, see the Reference section of the www.LearnColdFusionMX.com web site.


    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