Skip to main content
Inspiring
November 25, 2008
Question

Problems getting application variables to work

  • November 25, 2008
  • 4 replies
  • 840 views
I basically want to alternate a message when a page is loaded. First time loaded, message a. Second time loaded, message b.

I have a database table (testCount) holding one row in one column (theCount) and have tried the code attached below, but it just will not work. Any advice?
    This topic has been closed for replies.

    4 replies

    Inspiring
    November 26, 2008
    If your application is already running, the onApplicationStart() won't run. Plus you have the syntax wrong.

    The syntax should be
    <cffunction name ="onApplicationsStart>
    <cfscript>
    etc

    Try doing these things in the order specfied.

    1. Fix your syntax
    2. Comment out the toggle code in onRequestStart().
    3. In any template, set the three application variables and output the application.message.
    4. Uncomment the toggle code in onRequestStart()
    5. Back in the template, take away the code that set the 3 variables but keep the output code. Refresh the page a couple of times to make sure it works.
    Inspiring
    November 27, 2008
    Hi Dan,

    Thanks for you help again.

    Assuming the syntax in the attached code is correct, I have done the following:

    1. Clear application structure to start from fresh using <cfscript>structClear(application);</cfscript>
    2. Commented the toggle code in onRequestStart() from Application.cfc
    3. In index.cfm, I uncommented the setting of the three application variables commented out in the attached code.

    I then previewed the page and it outputs the value set for application.messageA as expected.

    4. Uncommented the toggle code in onRequestStart() from Application.cfc
    5. In index.cfm, removed the code that set the 3 variables and kept the output code. Refreshing the page once changes the output from the messageA value to the messageB value, but each subsequent refresh does not change the value.

    This is so frustrating. It should work, unless my syntax is wrong?
    Inspiring
    November 26, 2008
    The formatting is for cfscript. I'm too lazy to type angle brackets.

    In onApplicationStart() you code whatever you want to happen when the application is first run. In this case, you initialize a variable. You have to do that because you are going to toggle it in onRequestStart(), and you can't toggle something unless it's there.

    Inspiring
    November 26, 2008
    onApplicationStart

    application.message = application.messageA;


    onRequestStart

    if (application.message is application.messageA)
    application.message = application.messageB;
    else
    application.message = application.messageA;

    Then output application.message.
    Inspiring
    November 26, 2008
    Thanks again Dan, but I am unsure of how this would be utilised.

    All the examples of onApplicationStart I can see are of <cffunction name="onApplicationStart">.

    The formatting you have used, is it intended to be enclosed in a cfscript tag? I have tried that and get an error.

    Please advise.

    TIA,
    Paul
    Inspiring
    November 26, 2008
    Or would something along these lines work? This doesn't but something similar or am I barking up the wrong tree? This will be on just one page of my site.
    Inspiring
    November 25, 2008
    I think you'd be better off with an application variable instead of a db table. You can toggle the variable in the onRequestStart method and do whatever you need to do with it.
    Inspiring
    November 25, 2008
    Dan,

    Thanks for your reply. I did initially try it with application variables but could not get that to work either.

    Could you possibly give me an example of how I would do this correctly with application variables as I have not used them before. How would I use it in conjunction with onRequestStart as you mentioned?

    Many thanks in advance.

    Paul