Link to home
Start Free TrialLog in
Avatar of bhinshawnc1
bhinshawnc1

asked on

cffunction setting variables

Trying to modify some code that currently uses a settings.ini file to control the configuration. This won't work in final deployment environment.

Here's the current configuration

<cffunction name="getSettings" access="public" returnType="struct" output="false" hint="Returns application settings as a structure.">
<!--- load the settings from the ini file --->
            <cfset var settingsFile = replace(getDirectoryFromPath(getCurrentTemplatePath()),"\","/","all") & "/settings.ini">
            <cfset var iniData = getProfileSections(settingsFile)>
            <cfset var r = structNew()>
            <cfset var key = "">
            
            <cfloop index="key" list="#iniData.settings#">
                  <cfset r[key] = getProfileString(settingsFile,"settings",key)>
            </cfloop>
            
            <cfreturn r>
</cffunction>

settings.ini contains:
[settings]
password=
dsn=
dbtype=sqlserver
fromAddress=
perpage=20
tableprefix=

I want to call the dsn via variables defined in root application.cfm, normally in a query I'd call them like:

<cfquery name="GetSurveys" datasource="#dbname#"  username="#dbuser#" password="#dbpw#"  >

In the functions, they were originally built like:

<cfquery name="GetSurveys" datasource="#variables.dsn#">

A sample function:
<cffunction name="getSurveys" access="public" returnType="query" output="false" hint="Returns all the surveys.">
      <cfargument name="bActiveOnly" type="boolean" required="false" default="false" hint="Restrict to active surveys only. Also does the date restriction.">
            
      <!--- testing <cfargument name="dbname" required="yes" type="string" hint="from root application.cfm"> --->
                                    
      <cfset var qGetSurveys = "">
      <cfset var survey = "">
            
      <cfquery name="qGetSurveys" datasource="#variables.dsn#">
      <!--- testing <cfquery name="qGetSurveys" datasource="#dbname#"  username="#dbuser#" password="#dbpw#"  >--->
            select      id, name, description, active, datebegin, dateend, resultmailto, surveypassword, thankyoumsg
            from      #variables.tableprefix#surveys
            <cfif arguments.bActiveOnly>
            where      active = 1
            and (datebegin is null or datebegin < <cfqueryparam value="#now()#" cfsqltype="CF_SQL_TIMESTAMP">)
            and (dateend is null or dateend > <cfqueryparam value="#now()#" cfsqltype="CF_SQL_TIMESTAMP">)
            </cfif>
      </cfquery>
            
             <cfreturn qGetSurveys>
                  
      </cffunction>

How do I take the function getsettings and set the variables it returns for DSN to those variables I've already defined in application.cfm for
datasource="#dbname#"  username="#dbuser#" password="#dbpw#"  so they are passed to the function getsurveys via datasource="#variables.dsn#" ?



SOLUTION
Avatar of bwasyliuk
bwasyliuk

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bhinshawnc1
bhinshawnc1

ASKER

All helpful responses. Extra proints to bwasyliuk for the first to respond