>I've definitely avoided using arrays, and more recently, structures (you
>see, I didn't know about structures until just a while ago ...).
Basically,
>I'm having a few conceptual problems on just simply how to make, create,
>edit and add stuff to them.  But, even past that, I can't think of any
>practical applications for arrays and structures.  Obviously, there must be
>a ton, otherwise they wouldn't be in CF.


Arrays
-----------
Ben Forta's Web Application Construction Kit does a pretty good job of
explaining arrays. I *highly* recommend that you purchase a copy.  Read it.
Refer to it. Read it again.

Structures
-------------
Forta doesn't talk much about structures, so I'm going to try and fill in
the blanks.  Strutures are a way to store groups of similar data in a
single, easily referenced place.  Structures are similar to arrays but a
structure's elements are referred to by name rather than by their index.

A structure basically holds a series of name-value pairs.  The name must be
unique and is referred to as a key. For example, you could create a
structure to hold data about a product in an online store:

<CFSET stcItem = StructNew()>
<CFSET stcItem.ItemID = "1">
<CFSET stcItem.ItemName = "Product 1">
<CFSET stcItem.ItemPrice = "10.00">

The keys in this case are "ItemID", "ItemName", and "ItemPrice".

You could now display the ItemID with #stcItem.ItemID#, the Name with
#stcItem.ItemName# and the price with #stcItem.ItemPrice#.  It might help to
think about a structure as an "object" and the keys as "properties" of the
object.

The real advantage of structures is that you can nest them together.  The
above example is great if we're selling one item, but what about if we have
multiple items?  Simple: create a top level structure using the ItemID and
store another structure with the data properties as the associated value.

stcItems["1"].ItemName = "Product 1"
stcItems["1"].ItemPrice = "10.00"
stcItems["2"].ItemName = "Product 2"
stcItems["2"].ItemPrice = "20.00"

In this case stcItems is a structure with two keys: "1" and "2" (the
ItemIDs).  The value of each key is itself a structure with the keys
"ItemName" and "ItemPrice".

You would create the above structure like so:
<CFSET stcItems = StructNew()>
<!--- create the struct for item 1 --->
<CFSET stcThisItem = StructNew()>
<CFSET stcThisItem.ItemName = "Product 1">
<CFSET stcThisItem.ItemPrice = "10.00">
<!--- add this struct to the top level using the Item ID as the key --->
<CFSET StructInsert(stcItems, "1", stcThisItem)>

<!--- create the struct for item 2 --->
<CFSET stcThisItem = StructNew()>
<CFSET stcThisItem.ItemName = "Product 2">
<CFSET stcThisItem.ItemPrice = "20.00">
<!--- add this struct to the top level using the Item ID as the key --->
<CFSET StructInsert(stcItems, "2", stcThisItem)>

The advantage to doing this is that you can refer to any item in the top
level struct simply by knowing its ID.  For example, to access the details
of ItemID 35 (if it had been defined) we could simply use stcItems["35"].

If you've followed me this far (I'd be surprised... this is nowhere near as
concise as I had hoped it would be <g>) then you might already know one way
structures can be used in the real world:

1) Query database for all products
2) Create a nested structure like the examples before
3) Save the struct as a session or application variable.

Now, when a user requests the details for an item you can simply refer to
the data in the structure without re-querying the database.

Hope this makes helps a bit,
Seth Petry-Johnson
Argo Enterprise and Associates

------------------------------------------------------------------------------
Archives: http://www.eGroups.com/list/cf-talk
To Unsubscribe visit 
http://www.houseoffusion.com/index.cfm?sidebar=lists&body=lists/cf_talk or send a 
message to [EMAIL PROTECTED] with 'unsubscribe' in the body.

Reply via email to