CFLib.org – Common Function Library Project

ColumnLoop(query, columnName, theEval)

Last updated September 12, 2003

author

Shawn Seley

Version: 1 | Requires: CF5 | Library: DataManipulationLib

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:

<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:

/**
 * 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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson