Home.cfm ColdFusion Help

Home. cfm is a modified version of the template created in Chapters 1, 2, and 3. If you would like to know more about how to create the actual quiz, please refer to those chapters. In this chapter the quiz has been enhanced by using the application framework, and that is what the explanations will focus the new home. cfm template. It is similar to one created in except for the display of:
• The client usemame in the welcome message
• The server variable sess ion. season
• A score card for the current user and. all users

Listing 17.4 contains the code used to generate this template. An explanation of the code follows the listing.

The first query determines the total number of questions in the database and stores this value in an application variable. If this query has been run once, it will not be run again until the application structure is cleared, the server is shut down, or the application times out.

Then, all standard application variables are again set to non-persistent variables. In “this case the total number of questions is also stored in a non-persistent variable, so that it can be easily used in the next query without having to employ the use of CFLOCK, The next query is set as a session variable so that the quiz_results .  fm template can access the results without having to run another query. If a questi on_ i d exists, then that question and its related data are pulled from the database and stored in session. get_ question. Remember that you can store query recordsets in session variables. Otherwise, a random question is generated.

Then, after the page header information is displayed, the quiz question and its answers are displayed. Because this data is stored in a session variable, a CFLOCKtag must surround the entire block of code. You will notice that there is no more code in the home. cfm template, but the display shows a score card. The score card and its results are processed in the OnRequestEnd . cfm implate. Remember that the OnRequestEnd. cfm template is processed at the end of every application page in the current directory. The OnRequestEnd. cfm template will be explained later in this section.

quiz_results.dm

When the user answers a question and clicks the Score Question button on the home. cfm template, they are sent to the qui z_resul ts. cfm template. The user_answer is sent as a form field to qui zJesu 1ts. cfm. The purpose of the qui zJesu 1ts . cfm template is to
evaluate the user’s answers and provide feedback. Again, please refer to Chapter 3 for more information on the code used to generate these interactions. the response that is displayed when the user selects the incorrect answer. Listing 17.5 shows the code used to generate this interaction.

Again, the first thing that occurs in this template is to set all application variables to non-persistent variables to limit the number of (FLOCKtags used. The next major block of code determines whether the user _answer form field exists. If the user were to bookmark the page qui zJesu 1ts . cfm and go directly to it without answering a question, this would normally.cause an error. But instead, the template checks whether this form field exists and if it doesn’t, the user is prompted to go to the home. cfm template and the rest of the template is aborted. Remember that when a template
is aborted, the OnRequestEnd. cfm template is never processed

Then, the user_answer is evalu~ted against correct janswer stored in the session get_quest; on variable. If it is COllect, the user ingratulated; otherwise, they are prompted to answer the same question again.

And finally, the OnRequest End . cfm template is processed and displays the current score of the user and all other users. OnRequestEnd. cfm is discussed next.

OnRequestEnd.cfm

The final template in this application is the OnRequestEnd. cfm template. This template is executed at the end of each application page in the current directory. It will not be processed if the calling template aborts, exits, or returns an error. Listing 17.6 shows the code that is processed at the end of each template.

You have seen examples of OnRequestEnd. cfm in both home. cfm and quiz_results cfm . You did not see the results of OnRequestEnd. cfm in the other templates because they were aborted before the processing of OnRequest- End. cfm could occur.
In this application example, OnRequestEnd. cfm is used to track the total questions answered for the current user and all users, as well as the total correct answers for each. If these values have not been set, then they are set to 0 using the CFPARAMtag. Next, whenever the useranswersa question and goes to the OuizResults. cfm template, a count of l.is added to both the session . total_questons and application total questons. Then, if the user’s answer is correct, a count of 1 is also added to both the session . number_correct and app l icat; on . number_correct variables. The difference between the application variables and the session variables is that the application variables store the data of all users accessing the quiz. you can see that although the current user has answered only one question, someone else has also answered one question, making the application. total_questions count 2.

In the final block of code these scores are displayed and a standard footer is used.

Reviewing Variable Scope

In this chapter, you have learned how to create true applications in ColdFusion by using the application framework and persistent variable scopes. presents a review of these variable scopes.

Knowledge Base Articles

The following Knowledge Base articles can be found on Allaire’s Web site, http:// www.a11aire.com/Support/Know1 edgeBase/SearchForm. cfm. Each article addresses some aspect of working with the application framework and its variable scopes.

Article 288 “Changing Clientyariable Timeouts in CF 3”
Article 968Using Application Variables to Cache Static Query Results”
Article 8159 “Session Variables Do Not Work after 4 Upgrade”
Article 1152 “Differentiating Application, Session, and Client Variables”
Article 1113 “Speeding Up Deletion of Client Variables”

Where Do We Go from Here? .

In the next chapter, you will look at an important aspect of any ColdFusion application: error control. There are many possible sources of errors in ColdFusion applications, including mistakes in your ColdFusion code, invalid data provided by a user, and. problems in the configuration or operation of the server.’

However, it is unwise to let users to see the standard error messages generated by ColdFusion or to let your applications continue to operate with inv alid data from the user that could leadto strange or unexpected results. This is where error control comes into play, providing ways for you to write into your applications actions to take when errors occur. This is loosely tied to the topics discussed in this chapter because some types of error control are implemented in the App 1icati on. cfm file Another topic tied into the application framework is application security. To learn more about securing your applications, please refer to Chapter 30, which discusses limiting access to your applications.

Posted on November 16, 2015 in Implementing the coldFusion Web Application Framework

Share the Story

Back to Top
Share This