ColdFusionMX programming guide

Source: Internet
Author: User
Tags sql server driver

Phase III:Getting started with ColdFusionMX Programming


Preface

In the previous issue, we explained the basic management operations of ColdFusionMX and became familiar with the management interface layout of ColdFusionMX. In the last issue, we demonstrated two very short coldfusion programs, this topic will explain coldfusion's entry-level programming in detail, including database operations that are very troublesome for beginners in asp.

Before getting started, the preface of each issue will introduce you to ColdFusion development or other valuable tips, the first section introduces the Macromedia MX product policy and the position of coldfusionMX in this product policy. The second section introduces the version differences of ColdfusionMX, so what does the preface of this issue let us know? About the development of ColdFusion.

1995 is a memorable time, because the last century has happened in that year and month, and it is not just that much. If old-qualified developers recall that at that time, the web was dominated by html and cgi, the static page was html, and the dynamic program was over 90% cgi, this is what I think. When allire, a US brother, solves database access problems for its customers, it feels that the difficulty of writing and maintaining cgi programs is intolerable. As a result, Jeremy Allire, with a Computer talent, wrote a fast and easy-to-write dynamic scripting language called coldfusion in 1995. Once it was launched, it shocked the entire network industry. Later, the brothers saw the future of coldfusion and established Allire (which was subsequently acquired by macromedia ). In the next few years, half of the top 500 companies around the world have adopted coldfusion to solve different problems of the company. Coldfusion has become the first choice for developing industry-renowned enterprise-level solutions through continuous version modification and growth of developers. Currently, for product line unification and industrial standard unification, Macromedia has released ColdfusionMX to be fully compatible with the Java platform. Previously, many people often saw the word Neo, which is the name of macromedia encoding for the use of the next-generation coldfusion development industry solutions.

If you see the word neo, it is not difficult to understand Macromedia's ardent expectations for coldfusionMX. The name of the hero in the famous movie matrix is also Neo. The similarities above are just my speculation. Well, don't talk much. Let's get into the beginning of this issue.


Part 1 ColdFusionMX Basic tag Programming

I have read a lot of Chinese and English teaching materials, and I think the most effective way is to refer to the sample code segment of the instance to explain the process best. Therefore, the author does not describe each coldfuiosn tag in detail. You can view some references to learn about all the tag functions and syntax. For standard documentation, see the official documentation on the administrator management page after coldfusionmx is installed.

So what method does the author use to help friends who want to learn ColdFusionMX quickly enter the realm of coding? If all the tags are explained in order, the learner's interest may be decreased to varying degrees. You may have known the relationship between DreamweaverMX and ColdFusionMX In Macromedia MX in the previous tutorial. DreamweaverMX is released to provide a complete tool for ColdFusionMX application development, because it integrates the main functions of previous ColdFusion Studio, UltraDev, Dreamweaver, and HomeSite products. Therefore, using DreamweaverMX to create static pages can be said to only use less than 1/3 of the functions. The tutorials in this series start with the ColdfusionMX program functions integrated in DreamweaverMX, so that you can first learn how to develop the ColdFusionMX program in DreamweaverMX, then we will provide you with tips for using ColdFusionMX to develop advanced applications.

On the DreamweaverMX page, you can see the shortcut Object panel in the upper-right corner. The following three Object panels are CFML Basic, cfml flow, and CFML Advanced. The tutorial starts with CFML Basic.

After CFML Basic is selected, the following icons are displayed:

The icons from left to right represent CfServerVariables, CFQuery, CFOutput, CFInsert, CFUpdate, CFInclude, CFLocation, CFSet, CFParam, comment, variable symbol, and CFScript in sequence. Our tutorial also starts with CFSet. It is the simplest and most important Tag in ColdFusionMX.

Click the icon and the following code snippet appears: <cfset> the tag of this cfset is the unique tag of the variable set in Coldfusion. In ColdFusion, each unique Tag that is interpreted by the CFMX Server starts with cf, such as cfset and cfquery. The standard syntax format of cfset is:

The left side of the equal sign indicates the variable name, and the right side of the equal sign indicates the variable value. It's easy. If you want to create a variable named var1, assign the value hello, friend !, Then you need to write your program code as follows:

<Cfset var1 = "Hello, friend! ">

Then you need to set the Chinese character and write it like this:

<Cfset var1 = "Hello, friend! ">

Then, when you set a value for your variable, you do not need to add double quotation marks on both sides of the variable value. For example:

<Cfset PI = 3.1415926>. In addition, the function of the cfset tag can also be used for numerical calculation and dynamic transfer of variables. The encoding method for numerical calculation and variable jump is also very simple. Please refer to the following code snippet:

<Cfset PI = 3.1415926>

<Cfset number_1 = PI * 10>

<Cfset number_2 = number_1>

I think a friend with a little program experience should not be able to understand the above Code. It executes a PI value assignment and multiplication of PI variables. The result is assigned to number_1. Finally, the result of the number_1 variable points to the variable number_2.

In addition, Cfset has another usage. Let's take a look:

<Cfset var1 = "Test">

<Cfset "# var1 #" = "hello! ">

<Cfoutput> # test # </cfoutput>

What is the result output? The answer is: hello! This string. The reason is simple. hello! This string is paid to a variable called "# var1 #". This variable is equivalent to test, that is, the string "test" is automatically changed to a variable name.

Click the icon and the exclamation point indicates that the tag is cfparam. The basic function of the tag is the same as that of the cfset. It also assigns a value to the variable. What is the difference between the tag and the cfset? The difference is to check whether a variable exists. If so, a default value is given. See the Property setting window that appears after you click it:

We can see that cfparam has three attributes: name, Default, and Type. name indicates the name of the variable. Default indicates the initial value of the variable. The Default value is null. Type indicates the Type of the variable, CFMX sets the following types for cfparam:

The variable type already covers the variable types that we may use to encode with ColdFusion. For details about the Cfparam Tag, click tag info in the attribute settings window. In the future, you can click tag info in the attribute window. After cfparam is set, the following code line appears:

<Cfparam name = "URL. id" default = "1" type = "any">

This line of code is equivalent:

<Cfif not definede (URL. id)>

<Cfset URL. id = 1>

</Cfif>

Determine whether the URL. id variable exists. If it does not exist, a value of 1 is given. This function is often used with the cfform tag in the development, registration, and verification modules, which will be seen in the subsequent tutorials.

Click the icon to add the coldfusion-specific annotator to your cfm page: <! --- Content --->, for example:

<! ---

Set a variable named var_hello.

--->

The above code is ignored when the cfm is interpreted by the cf server. Good comments are necessary for developing large-scale and complex applications, and they are also the qualities that a good developer should possess.

Click the icon to display the # mark that is frequently encountered in the. cfm file. It is used to display the variable results in coldfusion, just like the previous program:

<Cfset var1 = "Test">

<Cfoutput> # var1 # </cfoutput>

Is to display the value of the var1 variable.

Click the icon to add coldfusion Server variable on the page. The pop-up interface in DreamweaverMX is:


CFMX supports the following types of Server variable:

The following describes the variables frequently used in Server variables. Server variables is CGI variables in coldfusion. It uses the Common Gateway Interface variable of the Server. CGI is a common script method. ColdfusionMX also supports some variable formats unique to CGI-Bin.

HTTP_REFERER: used to determine the URL from which the browser is located.

HTTP_USER_AGENT: determines the browser type used. Currently, several popular browsers can be identified.

QUERY_STRING: Is. cfm? The information that comes with the following parameters.

REMOTE_ADDR: IP address of the browser

You can view the official Reference for other variables.

Let's take a look at this Code:

<Cfoutput>

Hello!

Your browser is # CGI. HTTP_USER_AGENT #!

</Cfoutput>

The result shows the author's browser version:

Your browser is Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1 )!

Click the icon to use the most common tag: cfquery in coldfusion. This tag is used to complete database query.

Before you begin to explain database queries, I would like to remind developers to avoid using access and mysql when using coldfusionmx for development, the interaction between these two databases and coldfusionmx results in more or less Chinese characters. I hope you can use the SQL Server database or oracle database. Of course, it is okay to use access or mysql for English processing. In the early days, coldfusion5 was dealing with databases, and small databases such as access had no Chinese problems. Maybe the core code of coldfusion was rewritten, and the corresponding Chinese issue of jdbc came out, however, macromedia will soon fix this issue. When using cfquery, the author will use the access database for the convenience of explanation, but subsequent operations on changing database records using Chinese characters will be switched to SqlServer. Please note that.

For details about how to solve Chinese problems, refer to the following information:

Http://www.flashempire.net/showthread.php? S = 1871e826d3945c355df09e5ef8425a6e & threadid = 120844

After you click the cfquery tag, the interface shown in dwmx is as follows:

For the cfquery window, you can set the cfquery details in the red rectangle on the left of the graph. general is the longest setting we use, connection is the connection setting. Generally, some attributes of connection can be determined directly through the datasource setting in cfmx administrator. Persistent Queries indicates a continuous query that can be called from the cache. The standard syntax of cfquery is as follows:

Among them, the name attribute is required, that is, a name for the cfquery you define. This name attribute value will be used to find the relative query when you want to call it elsewhere. Datasource is the name of the data source from which the query comes, and the data source name is set by cfmx administrator. It is necessary to insert a section about how to set the data source. Open the cfmx administrator management interface, click Data Sources in data & Services, and then the Data source defined by cfmx administrator appears in the right area of the management interface, as shown below:

Indicates that microsoft access is used for some of the database drivers of 1, and two data source names set through windows odbc are selected in box 2. The data driver uses windows odbc socket. The selected area in the "3" dialog box is the data source defined by the Microsoft SQL Server Driver. Setting an access database is very easy. First, your access database can be placed in any location on your local hard disk, not under a directory of webserver. Then, you can enter a data source name from add new data source. The name can be a standard string and cannot start with a number. Then, select the Microsoft access Driver from the drop-down menu of the Driver and click add to go to the detailed settings page:

Basically, you do not need to set any advanced options for setting the access database, but you can click show advanced settings to modify the options if necessary, such as limiting database operations. Cfsnippets in is the default case database after coldfusionmx is installed, just like the northwind library automatically exists after the access software is installed. The cfquery example below is dominated by cfsnippets. SQL server only adds a few more steps. First, you must enter the database name of SQL server when setting the SQL server data source. Second, you must enter the ip address of SQL server, the local machine can use localhost. The most important thing is that you must enter a user name and password that can access the database, we recommend that you set a specific user name and password in SQL server, instead of using the default sa account.

Return to the cfquery attribute description. Other attributes are not required. For more information about attributes, see the reference manual of coldfusionMx.

Let's look at a simple cfquery code:

<Cfquery name = "test" DATASOURCE = "cfsnippets">

SELECT *

FROM Employees

Order by Emp_ID

</CFQUERY>

<Cfoutput query = "test">

# Emp_ID # | # FirstName # | # LastName # | # EMail # | # Phone # | # Department # <br>

</Cfoutput>

What is the result? Take a look at the following:

Surprisingly, it can be output cyclically !!! The number of cf codes is only a few lines. How many lines of code do asp, jsp, and php have to write to achieve the same effect? Everyone who has used it knows.

Let's look at a complex cfquery code:

<Cfquery name = "queryname" datasource = "cfsnippets">

SELECT *

FROM Employees

WHERE LastName = 'peterson'

</Cfquery>

<Cfoutput> # queryname. FirstName # <br>

# Queryname. LastName # <br>

# Queryname. Department # <br>

# Queryname. recordcount # <br>

# Queryname. currentrow # <br>

# Queryname. columnlist #

</Cfoutput>

<Cfquery Name = "queryname" datasource = "cfsnippets">

SELECT *

FROM Employees

</Cfquery>

<Table border = "1">

<Tr>

<Td> Last Name </td>

<Td> First Name </td>

<Td> Department </td>

</Tr>

<Cfoutput query = "queryname">

<Tr>

<Td> # LastName # </td>

<Td> # FirstName # </td>

<Td> # Department # </td>

</Tr>

</Cfoutput>

</Table>

The result is as follows:

We first select a piece of information about the employee perterson, and then list information about all employees. The second is to use queryname. recordcount to view the number of matched records, and queryname. currentrow to view the number of current records in the database. queryname. columnlist is used to represent a sort of columns in the database. Finally, we can see that several lines of code make the data in the entire employee table output cyclically in the html table. Do you already feel that coldfusion is simple and fast? More surprising functions will be provided in subsequent tutorials, including the built-in full-text search engine, Data Driven flash (image) of Generator, cf component, and web services.

The next tutorial will introduce the cf basic tag programming, including operating the cfinsert, cfupdate, cfinclude, and cflocation tags of the database. It will also explain how to compile coldfusion founction. Okay, everybody. Let's see you next time.


The second part discusses coldfusion

How to get the developer versionColdfusionMX

Http://www.flashempire.net/showthread.php? S = 783cb977b6551d0a2dc52c87555e0208 & threadid = 116459

ColdFusionMXFirst Security Vulnerability

Http://www.flashempire.net/showthread.php? S = 783cb977b6551d0a2dc52c87555e0208 & threadid = 121286

ColdFusionMXEnterprise standard documents

Http://www.flashempire.net/showthread.php? S = 783cb977b6551d0a2dc52c87555e0208 & threadid = 121621

Coldfusino MXLearning Experience

Http://www.flashempire.net/showthread.php? S = 783cb977b6551d0a2dc52c87555e0208 & threadid = 120616

CfmxChinese problem solution

Http://www.flashempire.net/showthread.php? S = 783cb977b6551d0a2dc52c87555e0208 & threadid = 120844

How to changeColdFusionMXPort

Http://www.flashempire.net/showthread.php? S = 783cb977b6551d0a2dc52c87555e0208 & threadid = 118258

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.