Combining Complex Data


As was the case with multidimensional arrays, you can combine complex data with itself. Rather than make arrays of arrays, as detailed in the preceding chapter, you can combine arrays with structures and structures with themselves.

Structures of Arrays

Combining structures with arrays supplies you with a perfect combination. You use arrays for their capability to maintain order. You use structures for their capability to store information by logical key names rather than numbers. By combining the two, you can create an ordered array of structures.

The following example contains sales figures by quarter for 2005 and 2006. sales is the structure containing two arrays (one for each of the two years). Each array contains four members (one per quarter) that store the sales figures.

Here is the code:

 <!--- Sales structure ---> <cfset sales=StructNew()> <!--- 2005 sales ---> <cfset sales.year2005=ArrayNew(1)> <cfset sales.year2005[1]=300000> <cfset sales.year2005[2]=475000> <cfset sales.year2005[3]=285000> <cfset sales.year2005[4]=575000> <!--- 2006 sales ---> <cfset sales.year2006=ArrayNew(1)> <cfset sales.year2006[1]=425000> <cfset sales.year2006[2]=325000> <cfset sales.year2006[3]=185000> <cfset sales.year2006[4]=450000> 

To refer to sales from 2006's third quarter (Q3), you'd use this element:

 #sales.year2006[3]# 

The advantage of this type of organization is that it simplifies the manipulation of data. For example, to display the sum of all sales in 2005, you could do the following:

 <cfoutput> #ArraySum(sales.year2005)# </cfoutput> 

Arrays of Structures

Shopping carts are often implemented as arrays of structures. The cart itself is an array (it holds a series of items), with each item a structure that provides maximum flexibility. Here is an example:

 <!--- Create shopping cart ---> <CFSET SESSION.cart=ArrayNew(1)> <!--- Add first item ---> <cfset SESSION.cart[1]=StructNew()> <cfset SESSION.cart[1].item="T-shirt"> <cfset SESSION.cart[1].color="red"> <cfset SESSION.cart[1].size="XL"> <cfset SESSION.cart[1].quantity="50"> <!--- Add second item ---> <cfset SESSION.cart[2]=StructNew()> <cfset SESSION.cart[2].item="cap"> <cfset SESSION.cart[2].color="navy"> <cfset SESSION.cart[2].size="adult"> <cfset SESSION.cart[2].quantity="25"> 

The shopping cart here is created in the SESSION scope, so that it persists. The cart is an array; each time an item is added to the cart, an array index is added to hold it. Individual cart items are structures that contain the item details.

Session state management and SESSION variables were covered in Chapter 11, "Session State Management."


Structures of Anything

Structures can contain arrays, and arrays can contain structures. But it doesn't stop there: Structures may also contain structures, as well as queries and any other data type.

In fact, some of the ColdFusion internal structures mentioned previously are structures that contain other elements.



Macromedia ColdFusion MX 7 Certified Developer Study Guide
Macromedia ColdFusion MX 7 Certified Developer Study Guide
ISBN: 0321330110
EAN: 2147483647
Year: 2004
Pages: 389
Authors: Ben Forta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net