Solved

Coldfusion loop and cffunction return

Posted on 2010-09-10
8
1,027 Views
Last Modified: 2012-05-10
How do I get a cffunction to return results from a looped query? The follow is my code and while one result is returned (the last query), I need a way for the return to contain all the results from the looped query.

<cfcomponent displayname="optionList.cfc" hint="I build arrays from comma serperated values.">
	
  <cffunction
    name="dataList"
    access="public"
    returntype="any"
    output="false"
    hint="I invoke the createArray component and return the results">
    
  <cfargument
      name="options" 
      type="string" 
      required="false"
      hint="I contain the comma seperated list">
    
    <cfset arrayObject = CreateObject("component","/com/createArray").listToArray(
          listData = arguments.options,
          Delimiter = ",",
          Qualifier = """"
          ) />
        
    <cfreturn getDescriptions( arrayObject=arrayObject )>
  </cffunction>
  
  <cffunction
    name="getDescriptions"
    access="private"
    returntype="any"
    output="false"
    hint="I get the option descriptions">
    
    <cfargument
      name="arrayObject"
      type="array"
      required="true"
      hint="I contain the array object">
      
    <cfloop index="i" from="1" to="#arrayLen(arrayObject[1])#">
    <cfset id = arrayObject[1][i]/>
        
    <cfquery name="qGetDescriptions" datasource="#request.dsn.source#">
          SELECT description
          FROM dbo.options
          WHERE id = '#id#'
     </cfquery>
       
    </cfloop>
      
    <cfreturn (#qGetDescriptions.description#)>        
  </cffunction>
  
</cfcomponent>

Open in new window

0
Comment
Question by:TheDesigners
  • 4
  • 3
8 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 33651159
I think a better approach is to just run a single query using IN (....). First loop to generate a list of id's.  Then pass that list to your query

<cfquery name="qGetDescriptions" datasource="#request.dsn.source#">
SELECT description
FROM dbo.options
WHERE id IN (  <cfqueryparam value="#id#" cfsqltype="cf_sql_varchar" list="true"> )
</cfquery>
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33651161
>> I build arrays from comma serperated values

Oops.. looks like you've already got a list ;-) So you can pass directly into your query.
0
 

Author Comment

by:TheDesigners
ID: 33651226
OK, passing the list to one query works to get all the results (tested in SMSS),  but what do I set the return to? When left as:

<cfreturn (#qGetDescriptions.description#)>

only the first "description" is returned. This is how I'm invoking the cffunction, and I'm displaying the results for testing using a cfdump:

<cfinvoke
      component="com.optionList"
      method="dataList"
      options="1,3,43,99"
      returnvariable="result">
</cfinvoke>
<cfdump var="#result#"/>

So, the query does get all the results, as tested in SMSS, but only the first description in the query is displayed in the cfdump.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:TheDesigners
ID: 33651932
Figured it out on my own. Solution was to place the query results into an array, return that array, and finally loop through that array to display the results:

<cfcomponent displayname="optionList.cfc" hint="I build arrays from comma serperated values.">
      
  <cffunction
    name="dataList"
    access="public"
    returntype="any"
    output="false"
    hint="I query a database table based on the variables passed in">
   
    <cfargument
      name="options"
      type="string"
      required="false"
      hint="I contain the comma seperated list">
   
    <cfquery name="qGetDescription" datasource="#request.dsn.source#">
        SELECT description
        FROM dbo.options
        WHERE id IN (<cfqueryparam value="#options#" cfsqltype="cf_sql_varchar" list="true">)
      </cfquery>
     
      <cfset results = arrayNew(1)>
     
      <cfloop query="qGetDescription">
            <cfset results[CurrentRow][1]=description>
      </cfloop>
     
      <cfset totalRecords = qGetDescription.RecordCount>
      <cfloop index="Counter" from="1" to="#totalRecords#">
            <cfoutput>
              Description: #qGetDescription.description#[1]
        </cfoutput>
      </cfloop>
       
      <cfreturn results>
  </cffunction>
 
</cfcomponent>
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 33658758
>> Solution was to place the query results into an array, return that array, and finally
>> loop through that array to display the results:

I think you're making it too complicated.  There is really no need for the extra conversion to an array. Just return the query object.  
ie <cfreturn qGetDescriptions>

When you want to display the results, loop through the query as usual.
<cfoutput query="qGetDescriptions">
     Description: #qGetDescription.description#
</cfoutput>

0
 

Author Closing Comment

by:TheDesigners
ID: 33700409
Thanks. Your solution works, The reason for the array is to be able to append additional information to the return.
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33702234
Okay.  As long as that's the only way to append the extra information.  Sometimes people create extra arrays or loops when they're not needed ;-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now