Home
Tools/Projects
ColdFusion
JavaScript
Articles/Tutorials
Creating Dynamic "onload" Events Using JavaScript
Client-to-Server Communication Using DHTML
Executing a DTS Package from CF/ASP/PHP/SQL
Creating a Custom ColdFusion Tag (101)
Structures
Downloads
Custom Tags
qForms JSAPI
Clients
About

Structures: Using CF-Based Structures
by Dan G. Switzer, II | 10/12/1999

Learning the ins and outs of structures can be beneficial for several reasons. Many of the native CF variable scopes (and the rumor is "all" scopes in the future) are stored as structures. The "application" and "session" scopes are both stored as structures. This means you can access the information within these scopes, just like they are normal structures.

However, there are also more creative uses for structures. One such example is storing dynamic variables in a structure, instead of using the setVariable() or evaluate() functions.

Before using structures were introduced into CF4, to display the value of a variable dynamically generated (such as alternating row colors), you had to something like the following:

Source Code

This works great, however the evaluate() function is extremely inefficient and can really slow down your scripts when done over a large number of records. One trick to speeding the process up, is to store the alternating row colors in a structure.

Let's take a look at the convential method for alternating color rows when looping through a query:

Source Code

The above code would output a table, alternating each row between white and a pale yellow color. This works great, but there's a more efficient and faster way to solve this problem using structures. Let's take a look at how we can solve the same problem using structures.

The first step we need to do is to create a structure in which we can use to hold all miscellaneous structure variables for the current script. We'll call the variable "stSettings".

Source Code

The next thing we need to do is to declare the alternating colors we're planning on using. We do that with the following:

Source Code

Now that we have the colors declared, let's do our loop.

Source Code

Notice, the code doesn't change much—only the part underlined has changed. However, we've gained a significant speed boost. To see just how much of an speed improvement is made, check out Example 1. (Warning: This script will take about 10-15 seconds to execute.)

You now probably have some good ideas on how structures could be used in your applications. In Part 3 we'll cover some really advanced ways of using structures in your applications.

  [< Previous ]   [Next >]  

If you have any questions about this article, please feel free to contact the author at dswitzer@pengoworks.com.

Copyright © 2024, PengoWorks.com. All Rights Reserved.