============== ColdFusion TIPS PLUS ColdFusion 01 Volume 01 Issue 06 =============================================================== http://www.cftipsplus.com ============== Issue 00045 ============== I. Comments: II. ColdFusion Tip: ColdFusion Arrays...What are they good for? By Kevin Schmidt, Web Technology Manager Allaire Certified Cold Fusion Developer schmidt@pwb.com III. ColdFusion Tip: Where is My Checkbox Variable? by Scott Knaub scott@web-mgt.com ============== Next Week's Issue will include the following:
Issue 00046
ColdFusion 5.0 By Kevin Schmidt, Web Technology Manager Allaire Certified Cold Fusion Developer schmidt@pwb.com
ColdFusion Tip: Application.cfm and OnRequestEnd By Nathan Stanford admin@nsnd.com
============== I. Comments:
We are in talks with several deveopers about the creation of our new CFJournal. You will be recieving a sample issue with an opportunity for a discount when you sign up for our new CFJournal.
============== Here is a Custom tag that you might be interested in. This tag was created by Kevin Schmidt. Take a look at it.
<CF_Mortgage_Calculator> http://www.fusemonkey.com/cf_mortgage_calculator/index.cfm
=============================================================== If you have any suggestions please email me at cftips@nsnd.com. ===============================================================
=============================================================== II. ColdFusion Tip: ColdFusion Arrays...What are they good for? By Kevin Schmidt, Web Technology Manager Allaire Certified Cold Fusion Developer schmidt@pwb.com ===============================================================
ColdFusion Arrays...What are they good for?
This week I am going to go over using an Array to store items in a shopping cart.
ColdFusion supports up to 3 dimensional arrays but for our example a 2 dimensional array will do the job. The first thing to do is create the Array. Since we want to save the shopping cart items over the course of a session we will scope the array accordingly. The code below creates an array:
<CFSET session.cart_items = ArrayNew(2)>
Now we have our array and are ready to start adding items to it. In order to do so we have to know the highest index value in the array and then add 1 to that number so we are not overwriting other items in the array. This can be accomplished by using the ArrayLen function to get the current length and then add 1 to that value. See the code below:
<CFSET i = ArrayLen(session.cart_items) + 1>
This works in all cases, even when there are no items in the array. (ColdFusion arrays start at 1 and not 0 like some other languages) Inserting information into ColdFusion arrays is accomplished by using the ArrayInsertAt function. So in order to insert our item_id and quantity we could use the following code.
<CFSET session.cart_items[i][1] = "#item_number#"> <CFSET session.cart_items[i][2] = "#item_quantity#">
Now we have our item_number and item_quantity stored in the array. What if we want to output the values in the array? By using <CFLOOP> we can loop over the contents of the array and output their values. See below:
<CFSET i = 1> <CFLOOP index="loop_count" from="1" to="#ArrayLen(session.cart_items)#" > <CFOUTPUT> Item Number: #session.cart_items[i][1]#<br> Item Quantity: #session.cart_items[i][2]#<br> </CFOUTPUT>
<CFSET i = i + 1> </CFLOOP>
The code above loops over every element in the array and outputs the values. This only a very basic example but it should get you started using arrays.
-- Next week I am going to go over a few of the features that are going to be available in ColdFusion 5.0
If you have any questions or suggestions for tips you would like to see feel free to email me at schmidt@pwb.com
Until next time Happy CFing.
Kevin Schmidt, Web Technology Manager Allaire Certified Cold Fusion Developer ===============================================================
============== III. ColdFusion Tip: Where is My Checkbox Variable? by Scott Knaub scott@web-mgt.com ==============
Here is a quick, clean method for determining the value of a checkbox field submitted by a Web form.
By definition a checkbox field on a Web form results in a Boolean value. The checkbox is either checked or unchecked. So typical values for checkbox fields are True or False, Yes or No, On or Off, etc.
An interesting challenge of Web forms is if a checkbox field is not checked, the form variable is never returned by the form. Up to now I've usually put a checkbox field on a form and then performed some validation before using the value, such as:
On the form: <FORM METHOD="POST" ACTION="Results.cfm" ENCTYPE="application/x-www-form-urlencoded">
<INPUT TYPE="CheckBox" NAME="Boolean_Value" VALUE="True">
Checkbox<BR>
<INPUT NAME="Submit" TYPE="submit" VALUE="Save">
Then on the Results.cfm template, check to see if the form variable was passed in from the form. If so, the value of the checkbox is set to True in a local variable. If the form variable was not passed in, the value of the checkbox is set to False in a local variable. Example:
<CFIF IsDefined("Form.Boolean_Value")>
<CFSET Variables.Checkbox_Value = "True">
<CFELSE>
<CFSET Variables.Checkbox_Value = "False">
</CFIF>
And then use the Checkbox_Value local variable throughout the Results.cfm template.
Another method is to overload the form with the Checkbox variable, by doing this:
<FORM METHOD="POST" ACTION="Results.cfm" ENCTYPE="application/x-www-form-urlencoded">
<INPUT TYPE="CheckBox" NAME="Boolean_Value" VALUE="True"> Checkbox<BR>
<INPUT TYPE="Hidden" NAME="Boolean_Value" VALUE="False">
<INPUT NAME="Submit" TYPE="submit" VALUE="Save">
This form will display exactly the same as the previous form. But, having the hidden field with the same name on the form ensures that the form variable is always passed to the results page. If the checkbox is not checked, the value of Form.Boolean_Value is "False". If the checkbox is checked, the value of Form.Boolean_Value is "True,False". Anytime you overload a form with the same variable name it submits all values as a comma delimited list in that one form variable. So now your validation could be:
<CFIF Form.Boolean_Value CONTAINS "True">
<CFSET Variables.Checkbox_Value = "True">
<CFELSE>
<CFSET Variables.Checkbox_Value = "False">
</CFIF>
And then use the Checkbox_Value local variable throughout the Results.cfm template.
Instead of using the CFIF validation, you can simply use the ListFirst() function and the form variable.
Remember when you overload your form with the same variable name for a checkbox the values are:
Checkbox is unchecked:
Form.Boolean_Value = "False"
Checkbox is checked:
Form.Boolean_Value = "True,False"
The order of the value list is determined by the order of the fields, from top to bottom, on the form. Since the checkbox field is first on the form and if it is checked when submitted, then its value will be first in the list. If unchecked, the form never sends its value.
Since the form submits both values in a comma delimited list, the ListFirst() function is a great way to determine the value of the checkbox field.
Checkbox is unchecked:
ListFirst(Form.Boolean_Value) = "False"
Checkbox is checked:
ListFirst(Form.Boolean_Value) = "True"
With this method you can simply use #ListFirst(Form.Boolean_Value)#
to represent the checkbox value throughout the rest of the Results.cfm template. If you prefer to assign the value to a local variable you can use:
<CFSET Variables.Checkbox_Value = ListFirst(Form.Boolean_Value)>
Assigning the value to a local variable is recommended if you will be using the value in many different places on the Results.cfm template. By assigning the value to a local variable you only run the ListFirst() function once instead of running that function each time you refer to the value. This helps to increase performance of your ColdFusion application.
by Scott Knaub scott@web-mgt.com ===============================================================
============== Publisher and Creator: Nathan Stanford, admin@cftipsplus.com
Web Developers: Andres Adamoli, andres@adamoli.com Dain Anderson, danderson@cftipsplus.com Hal Helms, hal.helms@TeamAllaire.com Kevin Schmidt, schmidt@pwb.com Sarah Durham, sarah@rmls.com Scott Knaub, scott@web-mgt.com Vincent Egt, vincent@e-dynamics.nl
============== Copyright (c) 2000 - 2001 CFTIPSPLUS.COM and NSND.COM Permission is granted to circulate this publication via MANUAL forwarding by email to friends provided that the text is forwarded in its entirety and no fee is charged. ==============
============== To unsubscribe: unsubscribe-cftips@nsnd.com
To subscribe: subscribe-cftips@nsnd.com ==============
____________________________________________________________ T O P I C A -- Learn More. Surf Less. Newsletters, Tips and Discussions on Topics You Choose. http://www.topica.com/partner/tag01