I have a database table that is a dictionary of defined terms -- key, value. I want to load the dictionary in the application scope from the database, and keep it there for performance (it doesn't change).
I gather this is probably some sort of "struct," but I'm extremely new to ColdFusion (helping out another team).
Then, I'd like to do some simple string replacement on some strings being output to the browser, looping through the defined terms and replacing the terms with some HTML to define the terms (a hover or a link, details to be worked out later, not important).
This is the code currently in the application.cfc file:
<cffunction name="onApplicationStart">
<cfquery name="qryDefinedTerms" datasource="mydsn">
SELECT term, definition FROM definedterms
</cfquery>
<cfset application.definedterms = Array(1)>
<cfloop query="qryDefinedTerms">
<cfset myHash = structNew()>
<cfset myHash.put("term", qryDefinedTerms.term)>
<cfset myHash.put("definition", qryDefinedTerms.definition)>
<cfset ArrayAppend(application.definedterms, myHash)>
</cfloop>
</cffunction>
The calling page attempts to use it as follows:
function ReplaceDefinitions(inputstring) {
for (thisdef = 1 ;
thisdef LTE ArrayLen(application.definedterms);
thisdef = (thisdef+1)) {
inputstring = Replace(inputstring,
application.definedterms(thisdef).term,
application.definedterms(thisdef).definition, "ALL");
}
return inputstring;
}
When I call the function, I get back: "Element DEFINEDTERMS is undefined in APPLICATION".
Edit: forcing a call to OnApplicationStart() worked, apparently Cold Fusion's application.cfc isn't like ASP.NET's web.config, changing it doesn't reset the application.