[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

cffunction setting variables

Posted on 2006-06-01
4
Medium Priority
?
492 Views
Last Modified: 2013-12-24
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#" ?



0
Comment
Question by:bhinshawnc1
4 Comments
 
LVL 7

Assisted Solution

by:bwasyliuk
bwasyliuk earned 800 total points
ID: 16810050
I have done something similar with session variables, but I assume the same would work with application variables

              <!--- Set up User's Client system parameters --->
              <cfquery name="ClientConfig_lst" datasource="#session.dsn#">
                clientconfig_lst
              </cfquery>
              <cfloop query="ClientConfig_lst">
                <cfset "session.cc_#ClientConfig_lst.Col_Cd#" = ClientConfig_lst.ColValue>
              </cfloop>

Here I use a query to retrieve their settings, but you will call your function and then loop through your struct that is returned.

In the loop where I set the session.cc_... variables, you can set Application.<var_name> variables.

Let me know if you need me to expand further...

by the way, this relies on your function returning a struct with key values the same as what you want your variables to be called.
0
 
LVL 10

Assisted Solution

by:rob_lorentz
rob_lorentz earned 600 total points
ID: 16810081
you can either pass the structure returned by getSettings into getSurveys....
or call getSettings from within getSurveys....
or assign the structure returned by getSettings to a request scope variables like....
<cfset request.globalSettings = getSettings()>
then in getSurveys access them like....
<cfquery name="qGetSurveys" datasource="#request.globalSettings.dsn#">
0
 
LVL 18

Accepted Solution

by:
Plucka earned 600 total points
ID: 16812197
bhinshawnc1,

Here's what we do

in application.cfm or application.cfc (CF7)

Just define your datasource in application scope

ie

<cfset application.datasource = "DataSourceName" />

You can then access this variable everywhere, including in components and you don't need to pass it around every where.

Regards
Plucka
0
 
LVL 3

Author Comment

by:bhinshawnc1
ID: 16812926
All helpful responses. Extra proints to bwasyliuk for the first to respond
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What You Need to Know when Searching for a Webhost Provider
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Integration Management Part 2
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question