View Single Post
Old 07-02-2009, 07:04 AM   PM User | #2
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
Hmm, seems your logic is a bit messed up. You're looping and repeatedly querying the database a number of times, but then you're only outputting the results of the last query call that is made. Put that cfoutput section that you have there inside the loop.

Also, inside the query, you are comparing column3 to the entire list of values that are held in the variable #myCSVlist#, when you should only be comparing column3 to the current list value that is being looped (that is, #i#, which by the way is a terrible variable name for looping over a list).

A much more efficient way to write this code and achieve the same result with only one database call would be this:
Code:
<cffunction name="yourFunctionName" access="public" output="yes" returntype="void">
    <cfargument name="myString" type="string" required="yes">
    
    <!--- Declare function local variables with the var keyword in the beginning
          of the function. Otherwise, they are treated as CFC scoped variables, or 
          page scoped variables, depending on where you are using the function. --->
    <cfset var myCSVlist = arguments.myString>
    <cfset var qData = "">

    <!--- 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 LIKE ( 
          #listQualify( myCSVlist, "'", ",", "char" )# 
        )
    </cfquery>
                    
    <cfoutput query="qData">
         #qData.column1#,<br />
         #qData.column2#,<br />
         #qData.column3#<br />
    </cfoutput>
</cffunction>
That listQualify() function just "qualifies" each list item by wrapping each one in single quotes, which is needed for the query if column3 is a string (varchar) column, and the list items are strings. However, it only does so for list items that have alphabetic characters in them.
Example Input: item1,item2,item3
Example Output: 'item1','item2','item3'

Hope that helps.

Greg
Gjslick is offline   Reply With Quote