User Guide

132 Chapter 8 Handling Complex Data with Structures
<input type="Submit" value="OK">
</form>
</body>
</html>
Example file addemployee.cfm
<!--- This file is an example of a custom tag used to add employees.
Employee information is passed through the employee structure (the
empinfo attribute). For databases that do not support automatic key
generation, you must also add the Emp_ID. --->
<cfif structisempty(attributes.empinfo)>
<cfoutput>Error. No employee data was passed.</cfoutput>
<cfexit method="ExitTag">
<cfelse>
<!--- Add the employee --->
<!--- If auto key generation is not supported,
you must also add the Emp_ID --->
<cfquery name="AddEmployee" datasource="cfsnippets">
INSERT INTO Employees
(FirstName, LastName, Email, Phone, Department)
VALUES
<cfoutput>
(
#StructFind(attributes.empinfo, "firstname")# ,
#StructFind(attributes.empinfo, "lastname")# ,
#StructFind(attributes.empinfo, "email")# ,
#StructFind(attributes.empinfo, "phone")# ,
#StructFind(attributes.empinfo, "department")#
)
</cfoutput>
</cfquery>
</cfif>
<cfoutput><hr>Employee Add Complete</cfoutput>
Looping through structures
You can loop through a structure to output its contents as illustrated in the following
example. Note that when you enumerate key-value pairs using a loop, the keys
appear in uppercase.
<!--- Create a structure and set its contents --->
<cfset departments=structnew()>
<cfset val=StructInsert(departments, "John", "Sales")>
<cfset val=StructInsert(departments, "Tom", "Finance")>
<cfset val=StructInsert(departments, "Mike", "Education")>
<!--- Build a table to display the contents --->
<cfoutput>