CFLib.org – Common Function Library Project

QueryToCsv(query [, headers] [, cols])

Last updated June 26, 2002

Version: 1 | Requires: ColdFusion 5 | Library: DataManipulationLib

 
Rated 5 time(s). Average Rating: 2.6

Description:
Transform a query result into a csv formatted variable.

Return Values:
Returns a string.

Example:

view plain print about
<cfset test_query = QueryNew("ValueField, DisplayField")>
<cfset QueryAddRow(test_query, 3)>
<cfset QuerySetCell(test_query,"ValueField","blue", 1)>
<cfset QuerySetCell(test_query,"DisplayField","my favorite color is blue", 1)>
<cfset QuerySetCell(test_query,"ValueField","changed the text for the heck of it", 2)>
<cfset QuerySetCell(test_query,"DisplayField","blah blah blah", 2)>
<cfset QuerySetCell(test_query,"ValueField","Louisiana", 3)>
<cfset QuerySetCell(test_query,"DisplayField","The State of Louisiana", 3)>
<cfoutput>
<pre>
#querytoCSV(test_query)#
</pre>
</cfoutput>

Parameters:

Name Description Required
query The query to transform. Yes
headers A list of headers to use for the first row of the CSV string. Defaults to cols. No
cols The columns from the query to transform. Defaults to all the columns. No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Transform a query result into a csv formatted variable.
 * 
 * @param query      The query to transform. (Required)
 * @param headers      A list of headers to use for the first row of the CSV string. Defaults to cols. (Optional)
 * @param cols      The columns from the query to transform. Defaults to all the columns. (Optional)
 * @return Returns a string. 
 * @author adgnot sebastien (sadgnot@ogilvy.net) 
 * @version 1, June 26, 2002 
 */

function QueryToCsv(query){
    var csv = "";
    var cols = "";
    var headers = "";
    var i = 1;
    var j = 1;
    
    if(arrayLen(arguments) gte 2) headers = arguments[2];
    if(arrayLen(arguments) gte 3) cols = arguments[3];
    
    if(cols is "") cols = query.columnList;
    if(headers IS "") headers = cols;
    
    headers = listToArray(headers);
    
    for(i=1; i lte arrayLen(headers); i=i+1){
        csv = csv & """" & headers[i] & """;";
    }

    csv = csv & chr(13) & chr(10);
    
    cols = listToArray(cols);
    
    for(i=1; i lte query.recordCount; i=i+1){
        for(j=1; j lte arrayLen(cols); j=j+1){
            csv = csv & """" & query[cols[j]][i] & """;";
        }        
        csv = csv & chr(13) & chr(10);
    }
    return csv;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
3 day(s) ago

Will Belden Will Belden added
longTime
9 day(s) ago

James Sleeman James Sleeman added
quickSort
19 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
22 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson