Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<CFSET> or <CFPARAM> ?

Status
Not open for further replies.

bobbyr

Programmer
Nov 30, 2000
66
US
I wrote this small shopping cart (1 item) and am encountering something weird. When I go to the initial "Purchase" page, the page seems to pause about half way through. I would assume this is where I have specified to write some session variables. In the application.cfm, this is what's written:

<CFAPPLICATION
NAME=&quot;WebsiteName&quot;
SESSIONTIMEOUT=#CreateTimeSpan(0, 0, 15, 0)#
SESSIONMANAGEMENT=&quot;yes&quot;>

<CFPARAM NAME=&quot;Session.Qty&quot; DEFAULT=&quot;&quot;>
<CFPARAM NAME=&quot;Session.Price&quot; DEFAULT=&quot;495.00&quot;>
<CFPARAM NAME=&quot;Session.ExtPrice&quot; DEFAULT=&quot;&quot;>
<CFPARAM NAME=&quot;Session.Weight&quot; DEFAULT=&quot;5&quot;>
<CFPARAM NAME=&quot;Session.TotalWeight&quot; DEFAULT=&quot;&quot;>
<CFPARAM NAME=&quot;Session.Tax&quot; DEFAULT=&quot;&quot;>
<CFPARAM NAME=&quot;Session.TotalPrice&quot; DEFAULT=&quot;&quot;>

On the purchase page, all that's written is <cfoutput>#DollarFormat(Session.Price)#</cfoutput>. Once I post the form, I reset the Session.Qty by <cfset Session.Qty = &quot;#Form.Qty#&quot;>.

My question is, do I need to even set the Session.Qty to &quot;&quot; in the application.cfm? Also, is using CFPARAM the right way to set the variable? The reason for setting these into session variables is that I need to pass them through 3 pages to checkout.
 
If you set session.qty to a value before the first time you use it, then technically you don't need to put it in the application.cfm file with the cfparam. In reality, I would recommend doing it exactly the way you have it. I've written a lot of similar applications and the top of your application.cfm is practically identical to the way I've structured mine so IMO, your use of the cfparam tag is good coding.

To answer your question of whethere it's necessary to set a session variable to null if you know you will set it on a specific page, consider this. If the person sits on a page for more than the session time such as in the case of a phone call, the next page they go to will create a new session. This means they will only have what session variables are defined in the application.cfm or the current page. This means that if you set the variable on page 1 and assume it will be there on page 3, the user will get a page error if they pause longer than 15 minutes (in your case) between pages 1 and 3. By putting it in the application.cfm file, all pages are guaranteed to have all the variables they expect. It's a nice catch-all way to handle them. There are other reasons involving less likely scenarios but I think this is the best reason to use the cfparams in your shopping cart.
 
What I tend to do in a situation like this is set the values in Application.cfm.
For example:
<CFPARAM
NAME=&quot;CLIENT.CoupID1&quot;
TYPE=&quot;any&quot; (or numeric)
DEFAULT=&quot;0&quot;>

This looks to see if this Client variable has a value and if it doesn't it sets a default to 0. This way I can do a <CFIF CLIENT.CoupID1 EQ 0>... and its easier than trying to check for NULL or &quot;&quot; or &quot; &quot; depending.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top