Fourth issue: COLDFUSIONMX Basic Tag Programming
Preface
Any kind of procedure is simpler also must have certain logic and the algorithm, ColdFusion is no exception. If just rely on a simple tag stack, it is impossible to truly achieve business logic. Moreover, I am very certain here that the simple is not equal to the weak function. At present, the network technology has developed to a performance-oriented era, is the development of each of the network applications, in order to achieve the logic at the same time, with the customer has a rich user experience is another pursuit of the goal. FLASH+FLASHREMOTING+CF serverside script is a very experienced development tool. And for development tools, many developers are more in the use of tools to do the article, Microsoft Vs.net Development Platform Integrity, oracle9i JDeveloper Development Java Program Wizard Way, Macromedia Dreamweavermx the support of numerous server-side scripting languages is a manifestation of the ease of use of tools. What we are going to explain to you today is the simple and easy-to-use ColdFusion loop. Many of the logic that can be programmed in CF is achieved through different loops in cf.
The preface also adds that the author has found a complete solution to the Chinese language when manipulating an Access database, and if any developer is interested, please check here:
The first part of Basic tag programming in-depth
In the last issue we talked about the first important label for manipulating the database, Cfquery. But we're just explaining how to use Cfquery for database queries, and now we're going to delve into the role of cfquery, and then we'll talk about the other two operating database tags Cfinsert and cfupdate.
Let's look at the construction of this table in the Cfsnippets database employees.
With the depth of the tutorial, the code is written in Verdana font number 8th, and use the basket color, please note the learner.
We can use Cfquery to do data insertion, writing in a very simple form, the way the code is as follows:
<CFTRANSACTION>
<cfquery name= "test" datasource= "Cfsnippets" >
Insert into Employees (firstname,lastname,email,phone,department)
Values (' Mj ', ' King ', ' mjking@263.net ', ' 1234567 ', ' technical ')
</CFQUERY>
<cfquery name= "test" datasource= "Cfsnippets" >
Select MAX (emp_id) as number_id
From Employees
</CFQUERY>
</CFTRANSACTION>
Insert Data Successful!
The new ID you inserted is:
<cfoutput query= "Test" > #number_id #</cfoutput>
Let's take a look at this piece of code above, where the most important part is the combination of two cfquery tags, which enables data insertion and the most recently inserted employee ID query. The first cfquery inserts a set of records into the Employees table, and in addition to the emp_id of the default employee ID, which is not processed, the other fields correspond to the insert. After inserting, the second cfquery does the emp_id of the record you just inserted into the query. For example, we want to develop an intranet staff module, it is possible to do a new employee input, immediately the system to inform the employee's work number, you can use this approach. But there is a problem, if you ask the server to do insert, there is another person just finished insert, is about to query out his ID, so it is possible to identify the ID is yours, because at this time, the system may respond to each other you have just completed the record of the insert Emp_ The value of the ID. To avoid this phenomenon, we used the cftransaction tag outside of the two cfquery.
Cftransaction is a coordinated transaction of the label, it handles different transactions as a whole, if one of the operations is unsuccessful, then all operations will be restored by the server (that is roll back), there is such a function is really good.
In addition, you can also use Cfquery to update the database, let's look at the following code:
<cfparam name= "url.number_id" default= ">"
<cfquery name= "test" datasource= "Cfsnippets" >
Select emp_id
From Employees
Where emp_id= #URL. number_id#
</Cfquery>
<cfoutput> #test .emp_id#</cfoutput>
<cfoutput> #test .recordcount#</cfoutput>
<CFIF test. RecordCount EQ 0>
No record of this employee
<Cfabort>
</Cfif>
<!---database Update--->
<cfquery name= "test" datasource= "Cfsnippets" >
Update Employees
Set firstname= ' Zerlot ',
Lastname= ' Ma ',
Email= ' zerlot.ma@hdtworld.com ',
Phone= ' 7654321 '
Department= ' Market '
Where emp_id= #URL. number_id#
</Cfquery>
<cfoutput> Update Successful! </cfoutput>
OK, we used cfquery to do the insert and update, so let's start with the Cfinsert and cfupdate tags.
Before we begin, we will briefly explain how access operates in Chinese, and there is no solution for displaying Chinese in an Access database at least until July 2002. However, inserting an Access database can be resolved in the following ways:
Create a application.cfm file under your application root, and add the following line of code:
<cfcontent type= "text/html; charset=gb2312 ">
Add the following code to the beginning of each of your CFM page templates:
<cfcontent type= "text/html; charset=gb2312 ">
<cfset setencoding ("URL", "gb2312") >
<cfset setencoding ("Form", "gb2312") >
Then you can use form forms or cfquery to insert Chinese without any problems. However, there is still a problem in querying and displaying a Chinese record from Access, and the author does not have an exact method at the moment. So the author's own tutorial using Access is simple, but if the developer uses CFMX to develop the application, persuading you to use SQL Server and Oracle's enterprise databases, these databases are not a problem.
Cfinsert and Cfupdate are two commonly used tags in coldfusion to manipulate them by clicking on the two icons cfbasic in Dreamweavermx.
Clicking on the icon will perform the setting function of the cfinsert tag. The clicked window looks like this:
The most frequently used attributes are the DataSource, TableName, and FormFields three properties, where DataSource is the name you set in the ColdFusion administrator data source settings. TableName is the table to update, FormFields is the name of the form control that corresponds to the name of the field in the database table (Note that you must be consistent to do so). Let's look at the following:
<!---The following code uses Cfinsert to update the data table without using the cfquery--->
<!---If the form has a submit action, start processing the Cfinsert action--->
<cfif isdefined ("form.posted") >
<cfinsert DataSource = "Cfsnippets"
TableName = "Comments"
FormFields = "email,fromuser,subject,messtext,posted" >
</cfif>
<!---use Cfquery to display insert data--->
<cfquery name = "Getcomments" DataSource = "Cfsnippets" >
SELECT
Commentid, EMail, Fromuser, Subject, Commttype, Messtext, Posted, processed
From
Comments
</cfquery>
<!---display data--->
<table>
<tr>
<td>from user</td><td>subject</td><td>comment type</td>
<td>message</td><td>date posted</td>
</tr>
<cfoutput query = "Getcomments" >
<tr>
<TD valign = top><a href = "mailt#email#" > #FromUser #</a></td>
<TD valign = top> #Subject #</td>
<TD valign = top> #CommtType #</td>
<TD valign = top><font size = "-2" > #Left (Messtext, #</font></td>)
<TD valign = top> #Posted #</td>
</tr>
</cfoutput>
</table>
<p>next, we ll offer the opportunity to enter a comment:
<!---input form--->
<form action = "cfinsert.cfm" method = "POST" >
<pre>
Email: <input type = "Text" name = "Email" >
From: <input type = "Text" name = "Fromuser" >
Subject:<input type = "Text" name = "Subject" >
Message:<textarea name = "Messtext" COLS = "ROWS" = "6" ></textarea>
Date Posted: <cfoutput> #DateFormat (now ()) #</cfoutput>
<!---dynamic display time--->
<input type = "hidden"
Name = "posted" value = "<cfoutput> #Now () #</cfoutput>" >
</pre>
<input type = "Submit"
Name = "" VALUE = "Insert my comment" >
</form>
Then we can see the results of the browser display the information we have just entered, the author does not give a screenshot, the purpose is to let everyone do their own to see the results, note that this database is access, the application does not support Chinese, if you want to test Chinese, You can build your own application to test Chinese (using SQL Server) by adding the following code to the beginning of each page:
<cfprocessingdirective pageencoding= "gb2312" >
<cfcontent type= "text/html; charset=gb2312 ">
<cfset setencoding ("URL", "gb2312") >
<cfset setencoding ("Form", "gb2312") >
Cfinsert we finished, then as the author himself, not very recommended to use Cfinsert, if you want to do a large number of database inserts, I hope that developers can flexibly use cfquery rather than cfinsert. Let's explain the cfupdate tag. Click the icon and the following interface appears:
You may see that Cfupdate's window settings are similar to Cfinsert's, yes, these two tags are labels that are proprietary to form forms. For the use of Cfupdate, the author does not want to spend a lot of ink, its use and Cfinsert is almost the same, we use a procedure to see the role of Cfupdate.
The program uses an official section of the script
<!---update database with cfupdate--->
<!---detect course_id This value, update data logging if present--->
<cfif isdefined ("form"). course_id ") >
<!---detection course_id is not a digital--->
<cfif not IsNumeric (form. COURSE_ID) >
<cfabort>
</cfif>
<!---do update--->
<cfupdate datasource= "Cfsnippets"
Tablename= "Courses"
Formfields= "descript, course_id" >
</cfif>
<!---use a query to see if the course_id is updated on the corresponding record by using the search technique to verify that the database is updated without having to open the database to view the--->
<cfquery name= "Getcourseinfo" datasource= "Cfsnippets" >
SELECT number, course_id, descript
From courses
<cfif isdefined ("URL. course_id ") >
WHERE course_id = #Trim (URL. COURSE_ID) #
</cfif>
ORDER BY number
</cfquery>
<title>cfupdate </title>
<body>
<cfif isdefined ("URL. course_id ") >
Course Number:<cfoutput> #GetCourseInfo .number#</cfoutput>
<form method= "POST" action= "cfupdate.cfm" >
<P> Course Description <BR>
<textarea name= "descript" cols= "rows=" "5" >
<cfoutput> #Trim (getcourseinfo.descript) #</cfoutput>
</textarea><br>
<input type= "Hidden" name= "course_id"
Value= "<cfoutput> #Trim (getcourseinfo.course_id) #</cfoutput>" >
<p><input type= "Submit" value= "click to Update" >
</form>
<cfelse>
<cftable query= "Getcourseinfo" HtmlTable colheaders>
<cfcol text= "<a href= ' cfupdate.cfm?" Course_id= #Trim (course_id) # ' >edit me</a> '
width=10 header= "Edit<br>this Entry" >
<cfcol text= "#Trim (number) #" width= "4" header= "Course number" >
<cfcol text= "#Trim (descript) #" width=100 header= "Course Description" >
</cftable>
</cfif>
</body>
The above procedure is an official piece of standard code that uses Cfupdate, which includes detecting parameters passed from the URL and updating the records of the database where the pass parameter course_id resides. This program is very incisive, I hope that developers can be very good research. Among them, the first access without parameters and display a record-level method is very practical, can be used in the actual development. Here, the author from my development experience again put forward a proposal, suggest you in the actual development of the regular operation of the database program when writing, can not cfinsert and cfupdate do not, because these two tag although convenient, but at the expense of flexibility and efficiency to achieve. I hope you use Cfquery to query, insert and update the database.
OK, below we relaxed, explain two very easy to understand the tag, these two tag in the process of the development of the use of the frequency is also very high. One is Cfinclude, the other is cflocation.
The icon on the left of the single hit will use the cfinclude tag in the ColdFusion and pop up the following window:
This tag is used to repeatedly use other page templates in a ColdFusion page, which is almost the same as include in ASP. For example, in ColdFusion to embed a site unified standard header and footer, one is header.cfm, the other is footer.cfm, then you can use Cfinclude to achieve.
<cfinclude template= "header.cfm" >
The use of Cfinclude also has drawbacks, with the continuous development of enterprise applications, business logic and performance needs to be separated more and more demanding, then we need to design components, so that completely mixed logic and performance of the cfinclude tag is not very applicable, But the current COLDFUSIONMX has its newest CFC (ColdFusion components), using this method to extract logic and performance. However, the advantages of Cfinclude is also the same as CF component can be a one-time modification of the page code to complete all the references in the site, in addition, Cfinclude can also contain a lot of application and session of the method.
Click the icon to the right of the two icons above to use another tag,cflocation from the ColdFusion. This tab plays a role in the page jump and it is often used with cfabort. For example, we do a user authentication program, when making some judgments, found that the user does not have permissions, then need to automatically send him back to the home page or a certain pages, this time, we will use cflocation this tag, for example:
<cfif form.registername EQ "" >
<cflocation url= "http://www.macromedia.com" addtoken= "No" >
<cfabort>
</cfif>
So when we use cflocation in Dreamweavermx, we see the following settings window:
Finally, let's take a look at the function in Coldfusionmx, in which the functions in Coldfusionmx are divided into 15 categories, including array, authentication,conversion,date/time,decision, Display and Formatting,dynamic Evaluation,international,list,mathematical,query,string,structure,system,xml, Other 15 kinds. These categories include 256 ColdFusion functions that should meet the needs of development applications, and you can also use your own combination to define your own function and component to meet your development needs. The author does not introduce the individual application of the function here, and will later explain some common funtions with the program in the tutorial. If developers have difficulty using or do not understand the use of the method, you can go to this place to see functions applications and examples:
Http://examples.macromedia.com/coldfusion/examples/index.cfm
Okay, so here's the tutorial, the next issue, we'll talk about the important CF flow in ColdFusion. See!!! Next term
Part II discussion on ColdFusion of forum
L Flash Remoting to realize flashmx and coldfusionmx Communication
http://www.flashempire.net/showthread.php?s=22c923d2c64f6f43642b0b7dd40ae2ab&threadid=124099
L How do you get The standard use case code for official tag
http://www.flashempire.net/showthread.php?s=22c923d2c64f6f43642b0b7dd40ae2ab&threadid=121883
L asp.net or cfmx ?
http://www.flashempire.net/showthread.php?s=22c923d2c64f6f43642b0b7dd40ae2ab&threadid=123532
Pet Store Deployment
http://www.flashempire.net/showthread.php?s=22c923d2c64f6f43642b0b7dd40ae2ab&threadid=122125
L Macromedia seek solution Partner
http://www.flashempire.net/showthread.php?s=22c923d2c64f6f43642b0b7dd40ae2ab&threadid=123151