|
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.
You can use a CFSET tag to define values for array indexes, as in the following example:
<CFSET arrayname[x]=queryname.column[row]>
In the following example, a CFLOOP is used to place four columns of data from a sample data source into an array, "myarray."
<!--- Do the query --->
<CFQUERY NAME="test" DATASOURCE="cfsnippets">
SELECT EMPLOYEE_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]=test.employee_id[CurrentRow]>
<CFSET myarray[CurrentRow][2]=test.LASTNAME[CurrentRow]>
<CFSET myarray[CurrentRow][3]=test.FIRSTNAME[CurrentRow]>
<CFSET myarray[CurrentRow][4]=test.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>
|
|