BackUp LevelNext

Populating an Array from a Query

When populating an array from a query, keep the following things in mind:

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>

BackUp LevelNext

allaire

AllaireDoc@allaire.com
Copyright © 1998, Allaire Corporation. All rights reserved.