
Populating Arrays with Data
123
Populating an array from a query
When populating an array from a query, keep the following things in mind:
•
Query data cannot be added to an array all at once. A looping structure is
generally required to populate an array from a query.
•
Query column data can be referenced using array-like syntax. For example,
myquery.col_name[1] references data in the first row in the column col_name of
the myquery query.
•
Inside a
cfloop query=
loop, you do not have to specify the query name to
reference the query’s variables.
You can use a
cfset
tag to define values for array indexes, as in the following
example:
<cfset arrayname[
x
]=
column
[
row
]>
In the following example, a
cfloop
places four columns of data from a sample data
source into an array, "myarray."
<!--- Do the query --->
<cfquery name="test" datasource="cfsnippets">
SELECT Emp_ID, LastName, FirstName, Email
FROM Employees
</cfquery>
<!--- Declare the array --->
<cfset myarray=arraynew(2)>
<!--- Populate the array row by row --->
<cfloop query="test">
<cfset myarray[currentrow][1]=Emp_ID[currentrow]>
<cfset myarray[currentrow][2]=LastName[currentrow]>
<cfset myarray[currentrow][3]=FirstName[currentrow]>
<cfset myarray[currentrow][4]=Email[currentrow]>
</cfloop>
<!--- Now, create a loop to output the array contents --->
<cfset total_records=test.recordcount>
<cfloop index="Counter" from=1 to="#Total_Records#">
<cfoutput>
ID: #MyArray[Counter][1]#,
LASTNAME: #MyArray[Counter][2]#,
FIRSTNAME: #MyArray[Counter][3]#,
EMAIL: #MyArray[Counter][4]# <br>
</cfoutput>
</cfloop>
Содержание COLDFUSION 5-DEVELOPING
Страница 1: ...Macromedia Incorporated Developing ColdFusion Applications MacroMedia ColdFusion 5 ...
Страница 58: ...38 Chapter 3 Querying a Database ...
Страница 134: ...114 Chapter 7 Updating Your Database ...
Страница 210: ...190 Chapter 10 Reusing Code ...
Страница 232: ...212 Chapter 11 Preventing and Handling Errors ...
Страница 238: ...218 Chapter 12 Using the Application Framework ...
Страница 262: ...242 Chapter 12 Using the Application Framework ...
Страница 278: ...258 Chapter 13 Extending ColdFusion Pages with CFML Scripting ...
Страница 320: ...300 Chapter 15 Indexing and Searching Data ...
Страница 336: ...316 Chapter 16 Sending and Receiving E mail ...
Страница 374: ...354 Chapter 18 Interacting with Remote Servers ...