当前位置: 首页 > 工具软件 > ColdFusion > 使用案例 >

coldfusion_ColdFusion入门

郤坚诚
2023-12-01

coldfusion

ColdFusion MX is the newest version of an already fantastic product developed by Macromedia, arguably the biggest and best producer of Web software out there. CFMX, as it’s commonly referred to, is built on top of J2EE (Java 2 Enterprise Edition), and as a result, is much more robust that previous versions of ColdFusion. If you are wondering whether or not you should upgrade that old copy of 3.1 you still have lying around, the answer is a resounding “yes!”

ColdFusion MX是Macromedia开发的已经非常出色的产品的最新版本,可以说是最大和最好的Web软件生产商。 众所周知,CFMX是在J2EE(Java 2企业版)的基础上构建的,因此,它比以前的ColdFusion版本更健壮。 如果您想知道是否应该升级仍然存在的3.1的旧版本,答案是肯定的“是!”。

If you’re just starting out, you probably have a bunch of questions. “Just what is ColdFusion? Do I need to know Java since it is built on J2EE? Why should I use CFMX instead of ASP or other free scripting language?” Let me answer these questions one at a time:

如果您只是刚入门,可能会遇到很多问题。 “什么是ColdFusion? 因为Java是基于J2EE构建的,所以我需要了解它吗? 为什么我应该使用CFMX而不是ASP或其他免费脚本语言?” 让我一次回答以下问题:

  1. First of all, ColdFusion MX is a fully functional application server. CFMX will work in conjunction with most popular Web server software available, including Apache, IIS and Deerfield Website. Think of it like this: let’s say you use IIS. In this scenario IIS will act as the batter in a baseball game. The pitcher is a Web user and ColdFusion is the catcher.

    首先,ColdFusion MX是功能齐全的应用程序服务器。 CFMX将与大多数流行的Web服务器软件一起使用,包括Apache,IIS和Deerfield网站。 这样想:假设您使用IIS。 在这种情况下,IIS将充当棒球比赛的击球手。 投手是Web用户,ColdFusion是捕手。

    In a typical “game”, the user “pitches” an http request for an html page and the batter (that’s the Web server software) hits the request back to the user. With CFMX, a user “pitches” a request for a ColdFusion file. The Web server steps back and let’s the catcher (ColdFusion) take the CFML request and pass it back to the user. I know this analogy is a bit odd but it usually helps illustrate where CFMX fits into the picture.

    在典型的“游戏”中,用户将“ http”请求发送给html页面,击球手(即Web服务器软件)将请求发回给用户。 使用CFMX,用户可以“提出”对ColdFusion文件的请求。 Web服务器退后一步,让捕获器(ColdFusion)接收CFML请求并将其传递回用户。 我知道这个比喻有点奇怪,但通常可以帮助说明CFMX适合图片的位置。

    Secondly, ColdFusion is a powerful server-side scripting language. And believe me, it’s as robust as any language out there. This language consists of tags similar to HTML tags, and FUNCTIONS similar to those found in Visual Basic.

    其次,ColdFusion是一种功能强大的服务器端脚本语言。 相信我,它和那里的任何语言一样强大。 该语言由类似于HTML标记的标记和类似于Visual Basic中的标记的功能组成。

  2. Third in this line of questions is “Why should I use CFMX instead of another free scripting language?”. I believe that Ben Forta answered that question with a bang in a recent issue of ColdFusion Developer’s Journal. The short version is “you get what you pay for” and CFMX, while it’s not cheap, does deliver a boat-load of features that would cost you many times the price of CFMX in another “free” language.

    问题的第三条是“为什么我应该使用CFMX而不是另一种免费的脚本语言?”。 我相信本·福塔(Ben Forta)在最近一期的《 ColdFusion开发人员杂志》中大声地回答了这个问题。 简短的版本是“您付钱”,而CFMX并不便宜,但确实提供了大量功能,而另一种“免费”语言的价格却是CFMX的许多倍。

  3. Do you need to know Java to use CFMX? No. CFML puts a simple and easy to use language between you and cumbersome Java.

    您需要了解Java才能使用CFMX吗? 不。CFML在您和繁琐的Java之间使用了一种简单易用的语言。

Ok, enough already! Let’s get coding!

好的,已经足够了! 让我们开始编码吧!

I’ve already said that CFML is similar to HTML, but what’s the difference between HTML and CFML? Simple. All ColdFusion tags start with the letters “CF”, allowing you to tell what’s static HTML and what’s ColdFusion code. When ColdFusion reads a CFML page and comes across a ColdFusion tag, it performs the specified task, and returns HTML to the end user. One of the reasons that CFML is so powerful is that it works perfectly with existing HTML. You may embed CF tags into your static HTML and even use ColdFusion to write HTML code for you in many cases. Let’s give it a try.

我已经说过CFML与HTML相似,但是HTML和CFML有什么区别? 简单。 所有ColdFusion标记均以字母“ CF”开头,从而使您能够分辨出什么是静态HTML以及什么是ColdFusion代码。 当ColdFusion读取CFML页面并遇到ColdFusion标签时,它将执行指定的任务,并将HTML返回给最终用户。 CFML如此强大的原因之一是,它可以与现有HTML完美配合。 在许多情况下,您可以将CF标签嵌入到静态HTML中,甚至可以使用ColdFusion为您编写HTML代码。 试一试吧。

第1课:CFML的简单技巧 (Lesson 1: The Simple Art of CFML)

Let’s start this off with perhaps the most basic and heavily used ColdFusion tag:<CFOUTPUT></CFOUTPUT> The “cfoutput” tag allows tells ColdFusion Server to “output” or display data between these tags. By itself, <CFOUTPUT> is useless, but if you combine it with double pound-signs (##), it becomes the basis for the CFML language structure. A completed output tag would appear something like this:

让我们从最基础和使用最频繁的ColdFusion标签开始: <CFOUTPUT></CFOUTPUT> “ cfoutput”标签允许告诉ColdFusion Server在这些标签之间“输出”或显示数据。 就其本身而言, <CFOUTPUT>是没有用的,但是如果将其与双井号(##)结合使用,它将成为CFML语言结构的基础。 完成的输出标签将显示如下:

<CFOUTPUT>#VariableName#</CFOUTPUT>

Note: You cannot place cfoutput tags inside other cfoutput tags as a rule. If you do, you will receive an error to tell you that you have invalidly nested cfoutput tags. There is a way and reason to do this, but it goes beyond the scope of this simple tutorial.

注意:通常,您不能将cfoutput标签放置在其他cfoutput标签内。 如果这样做,您将收到一条错误消息,告诉您嵌套无效的cfoutput标记。 有这样做的方法和理由,但这超出了本简单教程的范围。

Now, let’s produce a simple output. With most programming languages. you always seem to learn a “Hello World” script first, so let’s start with this:

现在,让我们产生一个简单的输出。 使用大多数编程语言。 您似乎总是总是首先学习“ Hello World”脚本,所以让我们从这里开始:

  1. Make sure that you have installed CFMX.

    确保已安装CFMX

  2. Be sure that ColdFusion MX Service is running.

    确保ColdFusion MX服务正在运行。
  3. Create a new folder named Lesson1 under the Webroot of the CFMX Server. C:CFUSIONMXWWWROOTLESSON1

    在CFMX服务器的Webroot下创建一个名为Lesson1的新文件夹。 C:CFUSIONMXWWWROOTLESSON1
  4. Open Cold Fusion Studio or another of your favorite text editors. I recommend ColdFusion Studio or Homesite for this function. You can also use Dreamweaver MX in code-view. This lesson will not refer to any of the visual CFML in that application.

    打开Cold Fusion Studio或您喜欢的其他文本编辑器。 我建议使用ColdFusion Studio或Homesite来实现此功能。 您也可以在代码视图中使用Dreamweaver MX。 本课将不涉及该应用程序中的任何可视CFML。
  5. Create a new blank CFML page. It will look the same as a blank HTML document.

    创建一个新的空白CFML页面。 它看起来将与空白HTML文档相同。
  6. Save this file into your new folder as INDEX.CFM

    将此文件另存为INDEX.CFM到您的新文件夹中
  7. In between the <BODY></BODY> tags, just as you would in HTML, enter the following code:

    <BODY></BODY>标记之间,就像在HTML中一样,输入以下代码:

    <CFSET MyWorld = 'Hello World'> 
    <CFOUTPUT>#MyWorld#</CFOUTPUT>
  8. View the page by opening your browser and entering your IP address (127.0.0.1 in most cases), followed by port 8500 and the folder Lesson1. It should appear like this:

    打开浏览器并输入IP地址(大多数情况下为127.0.0.1),然后输入端口8500和文件夹Lesson1,以查看页面。 它看起来应该像这样:

    http://127.0.0.1:8500/Lesson1/

    http://127.0.0.1:8500/Lesson1/

Great! Now, let’s break this code sample down:

大! 现在,让我们分解一下代码示例:

Breaking Down the Code Sample

分解代码示例

<CFSET MyWorld = 'Hello World'>

Whenever you need to use this variable on this template and this template only, you may "call" the variable into action with the above CFOUTPUT block. Once you leave this page, the variable dies and must either be reset with CFSET, or passed via the query string.

The name I chose, "MyWorld", is no magic name... it doesn't have any mystical programming properties at all. You could name your variable "clownshoes" if you felt the desire to do so.

Using the <CFSET> tag as I have above, there are a few basic guidelines that you may wish to follow.

  1. ColdFusion variable names are NOT case sensitive.

  2. ColdFusion variables are typeless. You do not need to declare whether a variable is a string, number, etc.

  3. Consider typing your variable names in "title case" where every word starts with a capital letter, similar to JackPoe or ColdFusion. This just makes it easier to read them. Feel free to adopt any coding practice you see fit, however, you should pick one naming practice and stick to it from here on out.

  4. Never ever use spaces or punctuation in your variable names... ever.

  5. Your variable name should not start with a number.

  6. Make your variable names easy to remember and short (when possible). This makes them easier to remember and quicker to type. Avoid creating variable names like "MyThirdVariableHere". If you have to type that a few dozen times it will get tiring quite fast.

So, to quickly recap and diagram a typical <CFSET> tag:

Now let's look at the <CFOUTPUT> block:

<CFOUTPUT>#MyScript#</CFOUTPUT>

You will notice that the variable name is encased in double pound signs or hash marks: ##. If you'll play along with the analogy, these are like ColdFusion's "fingerprints". The ColdFusion Server "dusts" a template, and anywhere it finds these "fingerprints", it says "Ah ha! I need to do something here."

In this case, the server receives a request to display the value of the variable named "MyWorld". So, rather than show you the word "MyWorld", it will show you the value of the variable, which is "Hello World" in this case.

Let's do another one -- this time we'll introduce two simple FUNCTIONS.

  • Now() - This function grabs the current date and time from the server, and formats them in a standard time-stamp manner.

    Now() -此函数从服务器获取当前日期和时间,并以标准时间戳方式对其进行格式化。

  • DateFormat(date, 'mask') - Using this function, we will override the default formatting of the date created with Now(), and tell ColdFusion how we want it formatted.

    DateFormat(date, 'mask') -使用此函数,我们将覆盖用Now()创建的日期的默认格式,并告诉ColdFusion我们希望如何格式化它。

<CFOUTPUT>#DateFormat(Now(), 'MM/DD/YYYY')#</CFOUTPUT>

This line of code will take the current date and reformat it to something like 04/12/2003. The cool thing is that you can treat this whole glob of code as regular text, like so:

这行代码将采用当前日期,并将其重新格式化为2003年12月12日。 很棒的事情是您可以将整个代码视为常规文本,如下所示:

Hello, Today Is <CFOUTPUT>#DateFormat(Now(),'MM/DD/YYYY')#</CFOUTPUT>.

Using this code in this manner, you'll see:

以这种方式使用此代码,您将看到:

Hello, Today is 04/12/2003

Add this code to your INDEX.CFM page and try it out. You can also add some font tags or other formatting HTML around this code to achieve your desired effect.

将此代码添加到您的INDEX.CFM页面,然后尝试一下。 您还可以在此代码周围添加一些字体标签或其他格式HTML,以实现所需的效果。

For more uses and formatting options of the DateFormat() function, consult the functions reference section at the end of this article.

有关DateFormat()函数的更多用法和格式设置选项,请查阅本文结尾的函数参考部分。

You will notice that we nested functions in this code sample.

您会注意到我们在此代码示例中嵌套了函数。

<CFOUTPUT>#DateFormat(Now(), 'MM/DD/YYYY')#</CFOUTPUT>

ColdFusion allows you to combine or "nest" functions forever. There's no limit to the number of functions you can combine.

ColdFusion允许您永远组合或“嵌套”功能。 您可以组合的功能数量没有限制。

Success with ColdFusion is based on two things:

ColdFusion的成功基于两点:

  1. Know your code. Spend the cash to buy a current CFML Language Reference book and study it. It really is interesting reading.

    了解您的代码。 花现金购买一本最新的CFML语言参考书并进行学习。 这真的很有趣。

  2. Practice. As with anything, practice makes perfect.

    实践。 与任何事物一样,实践使完美。

Note that in some cases you may want to display a pound sign to the user. To do this, you need to "escape the pound sign". This is done by placing two ## together with no information between them. ColdFusion will interpret this as a single character, not a variable to be processed.

请注意,在某些情况下,您可能希望向用户显示井号。 为此,您需要“转义英镑符号”。 这是通过将两个##放在一起而没有任何信息来完成的。 ColdFusion会将其解释为单个字符,而不是要处理的变量。

For example, if you use the <CFOUTPUT> tag around a table, and in your HTML tags you try to designate a BGCOLOR="#FFFFCC", ColdFusion will read this as the start of a process. To properly escape this you would enter BGCOLOR="##FFFFCC"

例如,如果在表周围使用<CFOUTPUT>标记,并且在HTML标记中尝试指定BGCOLOR="#FFFFCC" ,则ColdFusion将其视为过程的开始。 要正确地对此进行转义,您可以输入BGCOLOR="##FFFFCC"

第2课:使用ColdFusion显示数据库内容 (Lesson 2: Using ColdFusion to Display Database Contents)

Perhaps the biggest feature of ColdFusion is the amazingly simple way it interacts with databases. CFMX can tie into just about any database you can think of… Oracle, MS SQLServer, MySQL (my personal fave), MS Access, FileMaker, Sybase, Informix (my least favorite!) and more!

也许ColdFusion的最大功能是它与数据库交互的惊人简单方式。 CFMX几乎可以绑定到您可以想到的任何数据库中……Oracle,MS SQLServer,MySQL(我的最爱),MS Access,FileMaker,Sybase,Informix(我最不喜欢!)等等!

To work with CFMX and databases, you need to understand two things. ODBC and SQL.

要使用CFMX和数据库,您需要了解两件事。 ODBC和SQL。

  • ODBC stands for Open Database Connectivity, and is the basis on which ColdFusion connects to most standard databases.

    ODBC代表开放数据库连接,是ColdFusion连接到大多数标准数据库的基础。
  • SQL or “See-Quill” is the basic language that’s used to talk to databases. It’s a simple language that can make or break you as a developer. I suggest picking up a copy of Learn SQL in 10 Minutes by Ben Forta. This lesson assumes that you don’t know SQL, though, so I’ll keep it simple.

    SQL或“ See-Quill”是用于与数据库对话的基本语言。 这是一种简单的语言,可以使您成为开发人员,也可能使您失望。 我建议选择Ben Forta的《 10分钟之内学习SQL》的副本。 不过,本课假定您不了解SQL,因此我将使其保持简单。

First things first: we need a database! To save time, I’ve created a very simple database for you to download and use. It requires at least Microsoft Access 2000 — so make sure you have it before you continue.

首先,我们需要一个数据库! 为了节省时间,我创建了一个非常简单的数据库供您下载和使用。 它至少需要Microsoft Access 2000-因此在继续之前请确保已安装。

Now, we need to create a simple ODBC connection. This will require connecting to CFMX Administrator, the control panel that allows you to interact with the ColdFusion Application Server.

现在,我们需要创建一个简单的ODBC连接。 这将需要连接到控制面板CFMX Administrator,该面板允许您与ColdFusion应用程序服务器进行交互。

  1. Browse to 127.0.0.1:8500/cfide/administrator

    浏览至127.0.0.1:8500/cfide/administrator
  2. Enter your password. You should know this… if you installed the server properly. If you forget it, look at CFFAQ.com for details.

    输入密码。 您应该知道这一点……如果正确安装了服务器。 如果您忘记了它,请访问CFFAQ.com了解详细信息。

  3. In the left navigation, click the “Data Sources” link.

    在左侧导航中,单击“数据源”链接。
  4. In the Data Source Name field, enter “CFData” without the quotes.

    在“数据源名称”字段中,输入不带引号的“ CFData”。
  5. Open the dropdown list, select Microsoft Access and click ADD.

    打开下拉列表,选择Microsoft Access,然后单击添加。
  6. Browse the server to the database, or type in the path to the file manually.

    将服务器浏览到数据库,或手动输入文件的路径。
  7. Click SUBMIT.

    单击提交。

  8. If you did everything right, CFMX will display a message saying that the Data source was updated successfully, and you’ll see “OK” next to your source in the list of connected data sources.

    如果一切正确,CFMX将显示一条消息,说明数据源已成功更新,并且在已连接数据源列表中,您的源旁边将显示“确定”。

Now that you have an ODBC connection, let’s look at the database itself. This database is about as simple as possible, and contains just one table that holds a few records. In the real world, this rarely happens.

现在您已经有了ODBC连接,让我们看一下数据库本身。 该数据库尽可能简单,仅包含一个包含一些记录的表。 在现实世界中,这种情况很少发生。

If you’re unfamiliar with relational databases, Access is a good place to practice, but, a word to the wise: Access isn’t the best solution to your database needs. If you are looking at low traffic and minor data updates, then you’ll be fine with Access. If you are building a huge database against hundreds of thousands of records and/or expect high usage, consider trying MySQL… and if you can afford them trying SQLServer or Oracle.

如果您不熟悉关系数据库,Access是一个练习的好地方,但是明智的选择:Access并不是满足数据库需求的最佳解决方案。 如果您查看的是低流量和少量数据更新,则可以使用Access。 如果您要建立成千上万条记录的大型数据库和/或期望使用率很高,请考虑尝试使用MySQL…,如果您可以负担得起SQLServer或Oracle,则可以考虑使用。

Our database in this case contains a few records in the table named INFO. If you don’t know what a database table is, think of it as a big spreadsheet that holds all your data. Inside this table are records comprised of data stored in columns. It’s really quite simple.

在这种情况下,我们的数据库在名为INFO的表中包含一些记录。 如果您不知道数据库表是什么,可以将其视为容纳所有数据的大型电子表格。 该表内部是由存储在列中的数据组成的记录。 这真的很简单。

This database contains the following columns:

该数据库包含以下列:

NAME
 ADDRESS  
 CITY  
 STATE  
 ZIP  
 PHONE  
 EMAIL  
 NOTES  
 ID

Note that the ID column is your PRIMARY KEY. This is the standard way Access creates a unique identifier for each record in a database.

请注意,ID列是您的PRIMARY KEY 。 这是Access为数据库中的每个记录创建唯一标识符的标准方法。

Now, we’re going to use CFML to suck some of this data out onto the screen.

现在,我们将使用CFML将一些数据吸到屏幕上。

  1. Create a new folder under the CFMX Webroot, named LESSON2.

    在CFMX Webroot下创建一个名为LESSON2的新文件夹。
  2. In your text editor, create a new file and save it as ALLDATA.CFM

    在您的文本编辑器中,创建一个新文件并将其另存为ALLDATA.CFM
  3. Now, in that file, add this code between the BODY tags:

    现在,在该文件中的BODY标记之间添加以下代码:

    <CFQUERY DATASOURCE="CFdata" NAME="getall">   
    SELECT *   
    FROM Info   
    ORDER BY Name ASC   
    </CFQUERY>
  4. Then below this code, add the following HTML:

    然后在此代码下方,添加以下HTML:

    <table align="center" border="1">   
    <CFOUTPUT QUERY="getall">   
    <tr>   
      <td>#CURRENTROW#</td>   
      <td>#NAME#</td>   
      <td>#ADDRESS#<BR>#CITY# #STATE#, #ZIP#</td>   
      <td>#PHONE#</td>   
      <td>#EMAIL#</td>   
      <td>#NOTES#</td>   
    </tr>   
    </CFOUTPUT>   
    </table>
  5. Save the page and view it in your browser.

    保存页面并在浏览器中查看。

It’s not pretty yet — but it works!

还不是很漂亮-但可以!

Let’s review what this code does.

让我们回顾一下这段代码的作用。

The CFQUERY tag is the means by which CFML retrieves data from a database. The attributes we specified, DATASOURCE and NAME, are required by CFMX. The NAME attribute of this tag is specified by you, and can be any word that’s easy to remember. Query names cannot contain spaces or punctuation.

CFQUERY标签是CFML从数据库检索数据的方式。 CFMX需要我们指定的属性DATASOURCENAME 。 此标签的NAME属性由您指定,可以是任何容易记住的单词。 查询名称不能包含空格或标点符号。

Between the opening and closing CFQUERY tag is the SQL Query itself. This is not CFML, this is SQL, the database language. So just what did we tell the database to do? Let’s look at this in detail.

在开始和结束CFQUERY标记之间是SQL查询本身。 这不是 CFML,这是SQL数据库语言。 那么,我们告诉数据库要做什么呢? 让我们详细看一下。

  • SELECT *

    SELECT *

    This tells the database to get records. The asterisk means

    这告诉数据库获取记录。 星号表示

    all records. So In short, we’ve told the database to SELECT ALL.

    所有记录。 简而言之,我们告诉数据库SELECT ALL

  • FROM Info

    FROM Info

    Well, now that the database has been told to get all records, it follows that we need to tell it where to get them from. This line tells the database the name of the

    好了,既然已经告知数据库要获取所有记录,那么接下来我们需要告诉数据库从何处获取记录。 这行告诉数据库名称

    TABLE from which we want to select the data. In this case, we want the data from our table, Info.

    TABLE从中我们要选择的数据。 在这种情况下,我们需要来自表Info的数据。

  • ORDER BY Name ASC

    ORDER BY Name ASC

    This last line is really just gravy. It tells the database that, now that we’ve selected all data from the Info table, we want it organized by the column

    最后一行实际上只是肉汁。 它告诉数据库,既然我们已经从“信息”表中选择了所有数据,我们希望它按照该列进行组织

    NAME in ascending alphabetical order. If you wanted to sort the data by descending order, you’d use the abbreviation DESC.

    NAME按字母升序排列。 如果您想按降序对数据进行排序,则可以使用缩写DESC

What’s so great about CFML and SQL together, is that you can easily use CFML to augment your query to retrieve exactly the data you need… which brings us to the last part of this lesson.

CFML和SQL的共同之处在于,您可以轻松地使用CFML来扩展查询以准确检索所需的数据……这使我们进入了本课程的最后一部分。

第3课:使用CFML搜索数据库 (Lesson 3: Using CFML to Search the Database)

In part two, we used CFML to dump the entire database out to the screen. This works fine for a small database, but in real life it’s rarely useful, and can cause some serious performance problems. The easiest solution is to give users the ability to search the database to find exactly what they need. With CFML this is a snap.

在第二部分中,我们使用CFML将整个数据库转储到屏幕上。 这对于小型数据库来说很好用,但是在现实生活中它很少有用,并且可能导致严重的性能问题。 最简单的解决方案是使用户能够搜索数据库以找到他们真正需要的功能。 使用CFML,这很容易。

  1. Create a new file inside the lesson2 folder named INDEX.CFM

    在lesson2文件夹中创建一个名为INDEX.CFM的新文件
  2. Between the BODY tags of this document, add an HTML form like so:

    在此文档的BODY标签之间,添加一个HTML表单,如下所示:

    <div align="center">    
        
      <form action="search.cfm" method="POST">    
      Search for:<input type="text" name="k">    
    <input type="submit" value="Find!">    
      </form>    
        
    </div>
  3. Now create another new page named SEARCH.CFM

    现在创建另一个名为SEARCH.CFM的新页面
  4. Between the BODY tags we need to deal with validating the submitted form. Add this code:

    BODY标签之间,我们需要处理验证提交的表单。 添加此代码:

    <CFIF len(FORM.k) IS 0>    
      Oops!<br>    
      You need to enter some search criteria!<br>    
      <a href="javascript:history.back()">Go back</a>     
    and try again.    
        
      <CFELSE>    
        
    <CFQUERY DATASOURCE="CFdata" NAME="findem">    
    SELECT *    
    FROM Info    
    WHERE Name LIKE '%#FORM.k#%'    
    </CFQUERY>    
        
    <CFIF findem.recordcount IS 0>    
        
      Sorry!<br>    
      Your search for <CFOUTPUT>#FORM.k#</CFOUTPUT>     
    did not return any matches.<br>    
    <form action="search.cfm" method="POST">    
      Search again:<input type="text" name="k">    
    <input type="submit" value="Find!">    
      </form>    
        
        
    <CFELSE>    
    <!--- display your results --->    
        
    <table align="center" border="1">    
    <tr><td colspan="6" align="center">Your search returned     
    <cfoutput>#findem.recordcount#</cfoutput> matches.</td></tr>    
        
    <CFOUTPUT QUERY="findem">    
    <tr>    
      <td>#CURRENTROW#</td>    
      <td>#NAME#</td>    
      <td>#ADDRESS#<BR>#CITY# #STATE#, #ZIP#</td>    
      <td>#PHONE#</td>    
      <td>#EMAIL#</td>    
      <td>#NOTES#</td>    
    </tr>    
    </CFOUTPUT>    
    </table>    
    </CFIF>    
      </CFIF>
  5. Save the pages and try them out in your browser.

    保存页面并在浏览器中尝试。

Now, let's review the code. There's a lot more this time so we'll take it in chunks.

现在,让我们回顾一下代码。 这次还有很多,所以我们将其分批处理。

The INDEX.CFM page contains no tricks. It's just a simple HTML form. I named the text field "k" to stand for "keywords"...this is just how I do it.

INDEX.CFM页面不包含任何技巧。 这只是一个简单HTML表单。 我将文本字段命名为“ k”代表“关键字” ...这就是我的做法。

SEARCH.CFM contains a load of CFML you've probably never seen before. The first new tag you see is <CFIF>. This is the standard method we use to deal with conditional processing in CFML. This tag has several variations. For full details, check the reference at the end of this article. Basically what this specific use does, is stop users who try to submit for search form without entering any search keywords.

SEARCH.CFM包含您可能从未见过的CFML负载。 您看到的第一个新标签是<CFIF> 。 这是我们用于处理CFML中条件处理的标准方法。 该标签有多种变体。 有关完整的详细信息,请参阅本文末尾的参考。 基本上,这种特定用途是阻止试图提交搜索表单而不输入任何搜索关键字的用户。

Conditional processing allows a developer to say "if the sky is blue do this. Otherwise do this or this". This lets you trap events and possible errors, and control them to do exactly what you need.

条件处理使开发人员可以说“如果天空是蓝色,则执行此操作。否则,请执行此操作”。 这使您可以捕获事件和可能的错误,并控制它们以完全执行所需的操作。

<CFIF len(FORM.k) IS 0>

The "len(FORM.k) IS 0" portion of the above tag uses the LEN() function to determine the length of the submitted form field contents. In this case, if the length of the form field is zero, we will stop the page from processing.

上面标签的“ len(FORM.k) IS 0 ”部分使用LEN()函数确定提交的表单字段内容的长度。 在这种情况下,如果form字段的长度为零,我们将停止处理页面。

<CFELSE>

This tag is the "otherwise" condition. So if a user submits proper search criteria, CFMX will continue to process the page.

此标签是“其他”条件。 因此,如果用户提交正确的搜索条件,CFMX将继续处理该页面。

Let's take a closer look at that SQL. The first two lines should be familiar, but that last line might be new.

让我们仔细看看该SQL。 前两行应该很熟悉,但是最后一行可能是新的。

WHERE Name LIKE '%#FORM.k#%'

This is the WHERE CLAUSE in SQL. It's used to tell the database exactly what data you want selected. The basic syntax is WHERE ColumnName LIKE 'something'. The "something", if surrounded by quotes, must be a STRING VALUE -- a word or words. Without quotes, you tell the database to match on a number. In this case, we're letting ColdFusion fill in the blank with the form field value submitted by the user.

这是SQL中的WHERE CLAUSE 。 它用于告诉数据库确切要选择的数据。 基本语法是WHERE ColumnName LIKE '。 如果“东西”用引号引起来,则必须为“ STRING VALUE (一个STRING VALUE 。 不带引号的话,您告诉数据库要匹配一个数字。 在这种情况下,我们让ColdFusion用用户提交的表单字段值填充空白。

Wildcards: as with many languages, SQL allows you to use wildcards to increase the power of a search. In the above code we use the percent symbol to denote a wildcard. Inside the percent signs we place the submitted form variable "k". Here are some examples:

通配符:与许多语言一样,SQL允许您使用通配符来增加搜索的功能。 在上面的代码中,我们使用百分号表示通配符。 在百分号内,我们放置提交的表单变量“ k”。 这里有些例子:

  • '%variable%' means CONTAINS the variable value

    ' %variable% '表示包含变量值

  • 'variable%' means START WITH the variable value and ends in anything

    variable% ”表示以变量值开头,并以任何结尾

  • '%variable' means ENDS WITH the variable value and starts with anything.

    ' %variable '表示以变量值结尾,并以任何东西开头。

The next section of the results code that we need to examine is the next CFIF block. This code checks the RECORDCOUNT of the query, and if it is not zero -- meaning the query returns anything larger than the number zero -- the page will process. Next is a section that states how many records were found that match. Note the way the variable is called: FINDEM.RECORDCOUNT. This uses the query variable RECORDCOUNT, and only displays it once. If you were to place this code inside a CFOUTPUT that contained the QUERY attribute, the code would repeat for each record found! Not at all useful!

我们需要检查的结果代码的下一部分是下一个CFIF块。 此代码检查查询的RECORDCOUNT ,如果它不为零,则表示查询返回的内容大于零,该页面将被处理。 接下来的部分说明找到匹配的记录数。 注意变量的调用方式: FINDEM.RECORDCOUNT 。 这将使用查询变量RECORDCOUNT ,并且仅显示一次。 如果要将这段代码放在包含QUERY属性的CFOUTPUT内,那么对于找到的每个记录,该代码都会重复! 根本没有用!

Each ColdFusion query returns three automatic variables. CURRENTROW is the actual numbering of each record. For instance, if your query returns 50 records CURRENTROW would number them 1-50. RECORDCOUNT returns as a number the amount of records your query found. COLUMNLIST returns a comma separated list of the columns in the table searched.

每个ColdFusion查询返回三个自动变量。 CURRENTROW是每个记录的实际编号。 例如,如果您的查询返回50条记录,则CURRENTROW将其编号为1-50。 RECORDCOUNT以数字RECORDCOUNT返回查询找到的记录数量。 COLUMNLIST返回以逗号分隔的搜索表中列的列表。

So there you have it! The basics of ColdFusion and CFML. As you can see, it takes some work, but CFML is a simple and effective way to create any kind of Web application you can think of. Keep your eyes peeled for more on CF soon!

所以你有它! ColdFusion和CFML的基础。 如您所见,它需要一些工作,但是CFML是创建您可以想到的任何类型的Web应用程序的简单有效的方法。 很快就睁大眼睛,继续使用CF!

标签和功能参考 (Tag and Function Reference)
<CFOUTPUT></CFOUTPUT>
Displays the output of a database query, variable or other operation.
Syntax:
<CFOUTPUT></CFOUTPUT>
Displays the output of a database query, variable or other operation.
Syntax:

翻译自: https://www.sitepoint.com/get-started-coldfusion/

coldfusion

 类似资料: