CFLib.org – Common Function Library Project

ColumnLoop(query, columnName, theEval)

Last updated September 12, 2003

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

 
Rated 0 time(s). Average Rating: 0

Description:
Applies the passed evaluation to every cell in the specified query column. Allows versatile conversions, calculations, formatting and other alterations to an entire query column in one step. Use the variable "x" in the passed evaluation to denote the cell's original value. Especially useful for pre-processing a query before passing it to <cfchart>.

Return Values:
Returns a query.

Example:

view plain print about
<CFSET my_query = QueryNew("URL,Hits")>
<CFSET QueryAddRow(my_query, 1)>
<CFSET QuerySetCell(my_query,"URL""http://www.cflib.org/index.cfm")>
<CFSET QuerySetCell(my_query,"Hits","1000")>
<CFSET QueryAddRow(my_query, 1)>
<CFSET QuerySetCell(my_query,"URL""http://www.cflib.org/about.cfm")>
<CFSET QuerySetCell(my_query,"Hits","100")>
<CFSET QueryAddRow(my_query, 1)>
<CFSET QuerySetCell(my_query,"URL""http://www.cflib.org/resources.cfm")>
<CFSET QuerySetCell(my_query,"Hits","200")>

Query:<br>
<br>
<CFDUMP VAR="#my_query#">
<br>
<br>

Modify with ColumnLoop("my_query", "Url", "x=ReplaceNoCase(x, ""http://www.cflib.org"", """", ""ALL"")"):<br>
<br>
<cfset my_query2 = ColumnLoop(duplicate(my_query), "Url""x=ReplaceNoCase(x, ""http://www.cflib.org"", """", ""ALL"")")>
<CFDUMP VAR="#my_query2#">

Parameters:

Name Description Required
query Query object. Yes
columnName Column to be modified. Yes
theEval Evaluation to be performed. Use X to represent column data. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Applies simple evaluations to every cell in a query column.
 * 
 * @param query      Query object. (Required)
 * @param columnName      Column to be modified. (Required)
 * @param theEval      Evaluation to be performed. Use X to represent column data. (Required)
 * @return Returns a query. 
 * @author Shawn Seley (shawnse@aol.com) 
 * @version 1, September 12, 2003 
 */

function ColumnLoop(query, columnName, theEval) {
    var row = 0;
    var x = "";
    var rec_count = query.recordCount;
    for(row=1; row LTE rec_count; row=row+1) {
        x = query[columnName][row];
        querySetCell(query,columnname,evaluate(theEval),row);
    }
    return query;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

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

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

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

Ben Forta Ben Forta added
GetHostAddress
28 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