Using Arrays And Structures - Adobe COLDFUSION 9 Manual

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
The CFML Programming Language
<html>
<head>
<title>Your Order</title>
</head>
<body>
<cfif isDefined("Form.submit")>
<cfparam name="Form.itemID" default="">
<cfoutput>
You have ordered the following items:<br>
<br>
<cfloop index="i" list="#Form.itemID#">
ProductName: #Form["product_" & i]#<br>
Product Code: #Form["sku_" & i]#<br>
Quantity: #Form["qty_" & i]#<br>
<br>
</cfloop>
</cfoutput>
</cfif>
</body>
</html>
Save the file as ShoppingCartAction.cfm
3
4
Open ShoppingCartform.cfm in your browser, change the check box and quantity values, and click Submit.
Reviewing the code
The following table describes the code:
Code
<cfif isDefined("Form.submit")>
<cfparam name="Form.itemID" default="">
<cfoutput>
You have ordered the following items:<br>
<br>
<cfloop index="i" list="#Form.itemID#">
ProductName: #Form["product_" & i]#<br>
Product Code: #Form["sku_" & i]#<br>
Quantity: #Form["qty_" & i]#<br>
<br>
</cfloop>
</cfoutput>
</cfif>

Using Arrays and Structures

Adobe ColdFusion supports dynamic multidimensional arrays. Using arrays can enhance your ColdFusion
application code.
Adobe ColdFusion also supports structures for managing lists of key-value pairs. Because structures can contain other
structures or complex data types as it values, they provide a flexible and powerful tool for managing complex data.
Description
Run the CFML on this page only if it is called by submitting a form. This is not needed
if the form and action pages are separate, but is required if the form and action page
were one ColdFusion page.
Set the default Form.itemID to the empty string. This prevents ColdFusion from
displaying an error if the user clears all check boxes before submitting the form (so
no product IDs are submitted).
Display the name, SKU number, and quantity for each ordered item.
The form page posts Form.itemID as a list containing the value attributes of all the
check boxes. These attributes contain the shopping cart item IDs for the selected cart
items. Use the list values to index a loop that outputs each ordered item.
Use associative array notation to access the Form scope as a structure and use
expressions in the array indexes to construct the form variable names. The
expressions consist of a string containing the field name's field type prefix (for
example, "sku_"), concatenated with the variable i, which contains the shopping cart
ItemID number (which is also the loop index variable).
Last updated 8/5/2010
82

Advertisement

Table of Contents
loading

Table of Contents