Creating and Using Structures
127
Creating and Using Structures
This section explains how to use the structure functions to create and use structures
in ColdFusion. The sample code in this section uses a structure called employee,
which is used to add new employees to a corporate information system.
Creating structures
You create structures by assigning a variable name to the structure with the
StructNew function:
<cfset
mystructure
=StructNew()>
For example, to create a structure named employee, use this syntax:
<cfset employee=StructNew()>
Now the structure exists and you can add data to it.
Adding data elements to structures
After you create a structure, you add key-value pairs to the structure using the
StructInsert
function:
<cfset value=StructInsert(
structure_name, key, value
[
, AllowOverwrite
])>
The AllowOverwrite parameter is optional and can be either True or False. You can
use it to specify whether an existing key should be overwritten. The default is False.
When adding string values to a structure, enclose the string in quotation marks. For
example, to add a key, John, with a value, Sales, to an existing structure called
Departments, use this syntax:
<cfset value=StructInsert(Departments, "John", "Sales")>
The following example shows how to add content to a sample structure named
employee, building the content of the value fields dynamically using form variables:
<cfset rc=StructInsert(employee, "firstname", "#FORM.firstname#")>
<cfset rc=StructInsert(employee, "lastname", "#FORM.lastname#")>
<cfset rc=StructInsert(employee, "email", "#FORM.email#")>
<cfset rc=StructInsert(employee, "phone", "#FORM.phone#")>
<cfset rc=StructInsert(employee, "department", "#FORM.department#")>
Summary of Contents for COLDFUSION 5-DEVELOPING
Page 1: ...Macromedia Incorporated Developing ColdFusion Applications MacroMedia ColdFusion 5 ...
Page 58: ...38 Chapter 3 Querying a Database ...
Page 134: ...114 Chapter 7 Updating Your Database ...
Page 210: ...190 Chapter 10 Reusing Code ...
Page 232: ...212 Chapter 11 Preventing and Handling Errors ...
Page 238: ...218 Chapter 12 Using the Application Framework ...
Page 262: ...242 Chapter 12 Using the Application Framework ...
Page 278: ...258 Chapter 13 Extending ColdFusion Pages with CFML Scripting ...
Page 320: ...300 Chapter 15 Indexing and Searching Data ...
Page 336: ...316 Chapter 16 Sending and Receiving E mail ...
Page 374: ...354 Chapter 18 Interacting with Remote Servers ...