| By Bruce Van Horn | Article Rating: |
|
| October 5, 2000 12:00 AM EDT | Reads: |
6,608 |
It's been a long time since we announced this column back in May, but it's finally here! The purpose of the column is to expose you to some of the questions that Allaire's trainers are asked in the classroom and, hopefully, to give you the right answer.
Another goal is to give you, the readers of CFDJ, access to the same resources as our classroom students. You can e-mail your questions to us at AskCFDJ@sys-con.com. We'll circulate them among the Allaire Certified Trainers to get you the best answer. Don't be shy. If you have a question about using ColdFusion, let us know!
Since this is the first column, we haven't built up a backlog of questions, so I'd like to share a few I've encountered from students over the last few months.
Q:Is there a way to get CF Studio to save a file automatically when I browse using the internal browser?
A:Yes. This is a common problem. You're working on a page that you've previously saved; you make a few changes and browse again, but you don't see the changes because you forgot to save the file again before using Studio's internal browser. You can solve this problem by going to the Options menu and choosing "Configure External Browsers...". From that dialog box select the option to "Automatically save changes to the current document," then click OK. Next, return to the Options menu and choose Settings (F8). Select the Browse option on the left. Check the box labeled "Use External Browser Configuration for Internal Browser" and click OK. No more unsaved pages when you browse!
Q:I've set a local variable in Application.cfm, but it doesn't work in any of my custom tags. Why?
A:This can be frustrating, but it starts to make sense if you think about how CF processes custom tags. While the code Application.cfm is automatically included in the page that calls the custom tag, the CF server calls the custom tag itself as a separate request without including any of the code from Application.cfm. This explains why you get an error in your custom tag - that variable doesn't exist.
You can get around this problem in several ways. The first way would be to reference that variable from within the custom tag using the "caller" scope instead of the "variables" scope. For example, if you create a local variable in Application.cfm called "MyDSN" (<CFSET MyDSN = "MyDataSource">), you'd reference that variable in a regular CF page using the "variables" scope (<CFQUERY DATASOURCE= "#Variables.DSN#">). If that same page calls a custom tag, the code inside the tag has access to local variables that existed on the page that called it using the "caller" scope. For example, the code inside the custom tag page would look like this:
<CFQUERY DATASOURCE="#Caller.DSN#">
Another way to get around this problem would be to create the variable as an Application or Session variable instead of a local variable. For example, you could change the code in Application.cfm to <CFSET Application.MyDSN = "MyDataSource"> (assuming you've used the <CFAPPLICATION> tag to initialize the application framework). Once the variable is an application-level variable, it'll be available to your custom tags simply by referencing it as "Application.MyDSN" in your code.
Q:I have a CFQUERY that uses fully qualified column names:
<CFQUERY NAME="qGetDist" DATASOURCE="DistributorData">
SELECT Distributor.Distributor_Name, Country.Country_Name
FROM Distributor, Country
WHERE Distributor.Country_ID = Country.Country_ID
</CFQUERY>
When I try to output the value of Country.Country_Name, I get an error. How do I reference those columns in my CF page?
A:I remember making the same kind of mistake when I first started writing CF code. The answer is easy. You don't reference the column variable using the table name as the prefix, you use the name of the query as the prefix. In other words, it should be qGetDist.Country_Name, since qGetDist is the name you gave this query in the CFQUERY tag. Even though you prefix (or qualify) the database column with the name of the table in your SQL statement, the database gives back to CF just the name of the column (e.g., Country_Name). You can see this if you run the query using CF Studio's query builder tool. Only the column names come back to CF (which means you can't have duplicate column names in your query). When you reference query result columns as variables in your code, you need to prefix them with the name you specified in the NAME attribute of the CFQUERY tag.
Q:How can I find out what Session variables exist for a user?
A:The Session scope is actually a structure (a single variable that can hold many different values). The individual variables you set into the session scope are actually just "keys" within the Session structure. You can get a list of all the keys in a structure by using the StructKeyList() function (e.g., <CFSET SessionVars = StructKeyList(Session)>). This will give you a comma-separated list of all the keys for the session scope. You could also loop over the structure to get the same information:
<CFLOOP COLLECTION="#Session#" ITEM="KeyName">
<CFOUTPUT>
Session.#KeyName# = #Evaluate("Session."& KeyName)#<BR>
</CFOUTPUT>
</CFLOOP>
Q:How much memory should I allocate in the ColdFusion Administrator for template caching?
A:The goal behind this setting is to allocate enough memory so all your CF pages get cached. Setting this number too small creates a "rolling cache" situation in which new requests are added to the cache and older ones are rolled out. The guideline is basically this: calculate the total size of your CF pages (your CFML pages only, not image files, HTML pages, etc.) in kilobytes and multiply that number by five. The reason for doing this is that the generated p-code (the code that ColdFusion generates for your server to actually execute) is substantially larger than the saved CFML code.
Please send your questions about ColdFusion (CFML, CF Server or CF Studio) to AskCFDJ@sys-con.com.
Published October 5, 2000 Reads 6,608
Copyright © 2000 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Bruce Van Horn
Bruce Van Horn is president of Netsite Dynamics, LLC, a certified ColdFusion developer/instructor, and a member of the CFDJ International Advisory Board.

























