coldfusion

tech2024-06-11  4

coldfusion

Introduction: If you are reading this now then you should have already read Part I of this article, "ColdFusion Tutorial Part I – Database Integration" If you haven’t read Part I yet I suggest you do so as otherwise you may be unfamiliar with what I will talk about. In Part II of this article I will cover ColdFusion (CF) coding techniques. Everything I will cover will be found outside the <CFQUERY> tags, advanced coding within the <CFQUERY> tags will be found in Part III, "ColdFusion Tutorial Part III – Advanced SQL and Database Integration."

简介:如果您现在正在阅读这篇文章,那么您应该已经阅读了本文的第一部分,“ ColdFusion教程第一部分-数据库集成 ”。如果您还没有阅读第一部分,我建议您这样做,否则您可能不熟悉我会说什么。 在本文的第二部分中,我将介绍ColdFusion(CF)编码技术。 我将介绍的所有内容都可以在<CFQUERY>标记之外找到,在<CFQUERY>标记内的高级编码可以在第三部分“ ColdFusion教程第三部分–高级SQL和数据库集成”中找到。

1.处理复选框 (1. Dealing With Checkboxes)

If you experimented with what I covered in Part I you may have run into problems when taking input from a checkbox. This is because when a checkbox is not checked no value is submitted, not even a null value, so the CF interpreter ends up looking for a value that isn’t there. Fortunately there is an easy fix for this.

如果您尝试了我在第一部分中介绍的内容,那么从复选框中输入内容时,您可能会遇到问题。 这是因为如果未选中此复选框,则不会提交任何值,甚至不会提交空值,因此CF解释程序最终会查找不存在的值。 幸运的是,此问题很容易解决。

Figure 1.CFPARAM   <CFPARAM NAME = "addtolist" DEFAULT = "no">

图1. CFPARAM <CFPARAM NAME =“ addtolist” DEFAULT =“ no”>

By inserting the above line of code into your form processing page you are supplying the interpreter with a default value which fixes the problem.

通过将上面的代码行插入到表单处理页面中,您将为解释器提供解决该问题的默认值。

2.交替行颜色 (2. Alternating Row Color)

Often when displaying data from a database it is desired to alternate row background color for easy reading. This is achieved easy using only a few lines of code:

通常,当显示来自数据库的数据时,希望交替显示行背景色以便于阅读。 仅需几行代码即可轻松实现:

Figure 2. Alternating Row Color   <STYLE TYPE="text/css">   <!-- .row0 {background-color: green;}   .row1 {background-color: white;}   -->   </STYLE>   <TABLE>   <CFOUTPUT QUERY="QueryName">   <CFSET class="row#Int(QueryName.CurrentRow MOD 2)#">   <TR class = #class#>   <TD>#Field1#</td>   <TD>#Field2#</td>   </TR>   </CFOUTPUT>   </TABLE>

图2. Alternating Row Color <STYLE TYPE =“ text / css”> <!-.row0 {background-color:green;} .row1 {background-color:white;}-> </ STYLE> <TABLE> <CFOUTPUT QUERY =“ QueryName”> <CFSET class =“ row#Int(QueryName.CurrentRow MOD 2)#”> <TR class =#class#> <TD>#Field1#</ td> <TD>#Field2# </ td> </ TR> </ CFOUTPUT> </ TABLE>

Now lets examine the code. First you have some CSS stating that class .row0 will have such and such a background color and class .row1 will have a different color. Next you see a CFOUTPUT tag which should now be familiar to you, and after that is something which may not be. The CFSET tag is used to assign values to variables. We are using it to define a variable named class, the value of class is created by evaluating the function row#Int(QueryName.CurrentRow MOD 2)#. The first bit of that function is simply the text "row" which we can ignore. Then we see a # sign which marks the beginning of a CF expression. The Int function returns an integer when passed a value, the value it is using is QueryName.CurrentRow MOD 2. What this value is saying is "What is the remainder when the current row number is divided by 2." As you know dividing anything by 2 will result in either a 1 or a 0 as the remainder, so all even rows will deliver a remainder of 0, all odd rows will deliver a remainder of 1. So for even rows the variable class will be "row0" and for odd rows it will be "row1" which will allow you to alternate row colors.

现在让我们检查代码。 首先,您有一些CSS声明类.row0将具有这样的背景颜色,而类.row1将具有不同的颜色。 接下来,您将看到一个CFOUTPUT标记,现在您应该熟悉该标记了,此后可能就不再熟悉了。 CFSET标签用于为变量分配值。 我们使用它来定义一个名为class的变量,该class的值是通过评估函数row#Int(QueryName.CurrentRow MOD 2)#创建的。 该函数的第一位只是我们可以忽略的文本“行”。 然后我们看到一个#符号,它标志着CF表达式的开始。 当传递一个值时,Int函数返回一个整数,它使用的值是QueryName.CurrentRow MOD2。该值表示的是“当当前行号除以2时余数是多少”。 如您所知,将任何东西除以2将得到1或0作为余数,因此所有偶数行将交付余数0,所有奇数行将交付余数1。因此,对于偶数行,变量类将为“ row0”,对于奇数行,它将为“ row1”,这将允许您替换行颜色。

3. CFIF,CFELSE,CFELSEIF (3. CFIF, CFELSE, CFELSEIF)

If you’re familiar with any programming language you should be familiar with the concepts behind conditional code. I will now discuss the CF syntax for doing it. Every <CFIF> tag must have a matching </CFIF> tag, the <CFELSE> and <CFELSEIF> tags are completely optional. You may use as many <CFELSEIF> tags as needed, however you may use only one <CFELSE> tag per <CFIF> tag, and the <CFELSE> tag must always come last.

如果您熟悉任何编程语言,则应该熟悉条件代码背后的概念。 我现在将讨论执行此操作的CF语法。 每个<CFIF>标签必须具有匹配的</ CFIF>标签,<CFELSE>和<CFELSEIF>标签是完全可选的。 您可以根据需要使用多个<CFELSEIF>标签,但是每个<CFIF>标签只能使用一个<CFELSE>标签,并且<CFELSE>标签必须始终排在最后。

Figure 3.CFIF Example

图3. CFIF Example

<CFIF Login IS "True">   Welcome.   <CFELSEIF Login IS "False">   Sorry, we could not log you in.   <CFELSE>   The was a problem processing your request, please try again or contact the server admin.   </CFIF>

<CFIF登录信息为“真”,欢迎使用。 <CFELSEIF登录为“ False”>很抱歉,我们无法登录。<CFELSE>处理您的请求时出现问题,请重试或联系服务器管理员。 </ CFIF>

When comparing values within the CFELSEIF and CFIF tags you may use the following operators:

比较CFELSEIF和CFIF标记内的值时,可以使用以下运算符:

IS – Login IS "True" IS NOT – Login IS NOT "True" CONTAINS – "KY, MI, CA, FL" CONTAINS State DOES NOT CONTAIN – "MI, OH, CA" DOES NOT CONTAIN State GREATER THAN – Age GREATER THAN "13" LESS THAN – Age LESS THAN "62" GREATER THAN OR EQUAL TO – Age GREATER THAN OR EQUAL TO "13" LESS THAN OR EQUAL TO – Age LESS THAN OR EQUAL TO "62"

IS -登录“真” IS NOT -登录不是“真” 载 - “KY,MI,CA,FL”含有状态, 不包含 - “MI,OH,CA”不包含国家GREATER THAN -年龄GREATER THAN “ 13” 小于 –年龄大于“ 62” 等于或大于 –年龄大于或等于“ 13” 小于或等于 -年龄小于或等于“ 62”

Also you can combine statements using AND or OR, such as: age LESS THAN "20" and sex is "male".

您也可以使用AND或OR组合语句,例如:age LESS THAN“ 20”,性别为“ male”。

4. CFINCLUDE (4. CFINCLUDE)

To do SSI style includes using CF you should be using the <CFINCLUDE> tag.

要进行包括使用CF在内的SSI样式,您应该使用<CFINCLUDE>标记。

Figure 4. CFINCLUDE

图4. CFINCLUDE

<CFINCLUDE TEMPLATE = "include.htm">

<CFINCLUDE TEMPLATE =“ include.htm”>

I think it is very straightforward and needs no explanation. However unlike SSI you can only use relative paths to the file to be included.

我认为这非常简单,无需解释。 但是,与SSI不同,您只能使用要包含的文件的相对路径。

5.饼干 (5. Cookies)

A Cookie is a variable set on the clients computer, they are useful for tracking visitors, establishing logins, and a myriad of other things. To create a cookie in CF you use the <CFCOOKIE> tag. Then to read from the cookie you use code such as: #COOKIE.Name# where "name" is the name of the cookie.

Cookie是在客户端计算机上设置的变量,它们对于跟踪访客,建立登录名以及许多其他用途很有用。 要在CF中创建cookie,请使用<CFCOOKIE>标记。 然后使用以下代码从Cookie中读取代码:#COOKIE.Name#,其中“名称”是Cookie的名称。

Figure 5. Cookie Syntax

图5. Cookie Syntax

<CFCOOKIE NAME = "Name" Value = "Value" Expires = "Expiration Date">

<CFCOOKIE NAME =“名称”值=“值” Expires =“到期日期”>

An expiration date can be a definite date, such as "3/9/81", it can be a relative number of days "100" or it can be "Now" or "Never." Also in addition to the above there are some optional attributes to the CFCOOKIE tag. By including the word SECURE with no argument you specify that the cookie needs to be sent securely using SSL, if SSL is not available the cookie is not sent. By including DOMAIN (Domain = "webdevhq.com; webmasters-network.com") you can specify the domains the cookie applies to, and by including PATH you can specify the subset of the URL to which the cookie applies, when using PATH, DOMAIN is required.

到期日期可以是确定的日期,例如“ 3/9/81”,可以是相对天数“ 100”,也可以是“现在”或“从不”。 除上述内容外,CFCOOKIE标记还有一些可选属性。 通过在不带参数的情况下包括单词SECURE,可以指定需要使用SSL安全发送cookie,如果SSL不可用,则不发送cookie。 通过包含DOMAIN(Domain =“ webdevhq.com; webmasters-network.com”),您可以指定cookie应用于的域,通过包括PATH,您可以指定cookie应用于的URL的子集,当使用PATH时,必填DOMAIN。

Conclusion In this part of the article I have covered the most common CF coding techniques that are not applicable to the <CFQUERY> tag. In Part III I will cover advanced SQL and CF coding that directly affects your query.

结束语在本文的这一部分中,我介绍了不适用于<CFQUERY>标记的最常见的CF编码技术。 在第三部分中,我将介绍直接影响您的查询的高级SQL和CF编码。

翻译自: https://www.sitepoint.com/cold-fusion-tutorial-ii/

coldfusion

最新回复(0)