View Single Post
Old 07-02-2009, 07:47 PM   PM User | #4
Gjslick
Regular Coder

 
Join Date: Feb 2009
Location: NJ, USA
Posts: 476
Thanks: 2
Thanked 70 Times in 69 Posts
Gjslick will become famous soon enough
Whoops, sorry about that query, was kinda late last night when I was writing it! I meant to use the 'IN' operator instead of the 'LIKE' operator.

It really should be:
Code:
SELECT * FROM myTable
WHERE column3 IN ( 
  #listQualify( myCSVlist, "'", ",", "char" )# 
)
So for example, if the variable #myCSVlist# held "item1,item2,item3" then the final query text sent to the database would be exactly:
Code:
SELECT * FROM myTable
WHERE column3 IN ( 
  'item1','item2','item3'
)
Which, if you don't know much about database querying, translates into:
Code:
SELECT * FROM myTable
WHERE 
  column3 = 'item1'
  OR column3 = 'item2'
  OR column3 = 'item3'
---

Now, if column3 is a numeric-typed column, and all of the items in #myCSVlist# are also numbers, you could write the query without listQualify, such as:
Code:
SELECT * FROM myTable
WHERE column3 IN ( 
  #myCSVlist# 
)
The reason you could do this is because numbers do not have to be surrounded by single quotes. So, if the variable #myCSVlist# held "1,2,3" then the final query text sent to the database would be exactly:
Code:
SELECT * FROM myTable
WHERE column3 IN ( 
  1,2,3
)
---

As far as returning the data to a flex app, there are only two complex data types in coldfusion: array and structure. Arrays are indexed by numbers, and structures have keys and values. It seems you want to return an array of structures.

By the way, make sure you set the returntype attribute in the cffunction tag to be returntype="array".

Here's some example code of it all put together:
Code:
<cffunction name="yourFunctionName" access="public" output="yes" returntype="array">
    <cfargument name="myString" type="string" required="yes">
    
    <cfset var returnArray = arrayNew( 1 )>
    <cfset var myCSVlist = arguments.myString>
    <cfset var qData = "">   <!--- Will be a query variable --->
    <cfset var tempStruct = structNew()>

    <!--- Query the database.  This query gets each database row where
          column3 matches a value in #myCSVlist# --->
    <cfquery name="qData" datasource="#dsn#">
        SELECT * FROM myTable
        WHERE column3 IN ( 
            #listQualify( myCSVlist, "'", ",", "char" )# 
        )
    </cfquery>
  
    <!--- Build the array.  Each element of the array is a structure. --->
    <cfloop query="qData">
        <cfset tempStruct = structNew()>
        <cfset tempStruct.column1 = qData.column1>
        <cfset tempStruct.column2 = qData.column2>
        <cfset tempStruct.column3 = qData.column3>
    
        <cfset arrayAppend( returnArray, tempStruct )>
    </cfloop>
     
    <!--- Return the array to the calling code --->
    <cfreturn returnArray>
</cffunction>
Now this will return an array, where each element of the array is a structure. So we can access the data held in this array like this:
Code:
<!--- The data held in column2 of the first row of data will be accessed as: --->
returnArray[ 1 ].column2

<!--- The data held in column1 of the second row of data will be accessed as: --->
returnArray[ 2 ].column1
Just a note, coldfusion arrays are 1-based (that is, the first element of an array is 1 instead of 0). In flex, arrays are 0-based, so when accessing the data in flex, the first row of data will be returnArray[ 0 ] instead of returnArray[ 1 ].

Hope that all helps! Let me know if you have any problems. Wish I had a copy of your data and such so I could actually test the code out, but it should in theory work, lol.

Greg
Gjslick is offline   Reply With Quote