学习coldfusion总结(摘抄网友相随心声)
demol2.cfm文件如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<!--- 让coldfusion强制转换成utf-8编码格式--->
<cfprocessingdirective pageencoding="utf-8">
<!--- 用cfset标签定义局部变量--->
<cfset var1="first" />
<!--- 用cfparam标签定义局部变量--->
<cfparam name="var2" default="second" />
<!--- 输出到页面语句 --->
<cfoutput>today is tuestday!</cfoutput><br />
<cfoutput>The First Variable:#var1#</cfoutput><br />
<cfoutput>The Second Variable:#var2#</cfoutput><br />
<!--- 条件语句cfif、cfswitch--->
<!--- 条件表达1 --->
<cfif var1 eq var2>
var1 is equal to var2!<br />
<cfelse>
var1 is not equal to var2!<br />
</cfif>
<!--- 条件表达2 --->
<cfif "t" eq "tfq">
t is equal to tfq!<br />
<cfelseif "tfq" eq "tfq">
tfq is equal to tfq!<br />
<cfelse>
tfq is not equal to tfq!<br />
</cfif>
<!--- 布尔条件表达式 --->
<cfif 1 Is 1>
1=1 <br />
<cfelse>
1!=1
</cfif>
<!--- cfswitch条件词句,在此结构中要注意expression这个关键字,千万别写错了 --->
<cfset var3="tangfuqiang" />
<cfswitch expression=#var3#>
<cfcase value="tang">
tang is equal to tangfuqiang <br />
</cfcase>
<cfcase value="tangfu">
tangfu is equal to tangfuqiang!<br />
</cfcase>
<cfcase value="tangfuqiang">
tangfuqiang is equal to tangfuqiang!<br />
</cfcase>
<cfdefaultcase>
all values are not equal to tangfuqiang!<br />
</cfdefaultcase>
</cfswitch>
<!--- 循环语句 from=1 to =14与java 中i=1;i<=14 --->
<cfloop index="loopCout" from=0 to =14>
<!--- 如果索引等于0,则打印0=0且跳出循环 --->
<cfif #loopCout# Is 0>
0=0 <br />
<cfbreak>
<cfelse>
0!=0 <br />
</cfif>
the cfloop index is=<cfoutput>#loopCout#</cfoutput><br />
</cfloop>
<!--- 创建一个数组 --->
<cfset myArray=ArrayNew(1)>
<!--- 初始化myArray数组 --->
<cfset ArraySet(myArray,1,5,123)>
<!--- 给数组第四个元素设置值为tang --->
<cfset myArray[4]="tangfuqiang">
<cfset foundit=false>
<cfset i=1>
<cfset f=0>
<!--- 打印数组长度 --->
<cfoutput>#Arraylen(myArray)#</cfoutput><br />
<!--- cfdump调试打印出数组元素, <cfabort>是让后面的不执行,<cfexit>标记控制对当前一个自定义标记处理--->
<cfdump var="#myArray#">
<!--- 数组长度为4的值--->
<cfoutput>the first:#myArray[4]#</cfoutput><br />
<cfloop condition="(Not foundit) And (i lt Arraylen(myArray))">
<cfset f=f+0>
<cfif myArray[i] eq "tangfuqiang">
<cfset f=f+1>
<cfset foundit=true>
<cfbreak>
</cfif>
<cfoutput>#i# - #myArray[i]#<br></cfoutput>
<cfset i=i+1>
</cfloop>
<cfoutput>
i is #i#<br />
f is #f#<br />
foundit id #foundit#<br />
</cfoutput>
<!--- Set the variable CountVar to 0. --->
<cfset CountVar = 0>
<!--- Loop until CountVar = 5. --->
<cfloop condition = "CountVar LESS THAN OR EQUAL TO 5">
<cfset CountVar = CountVar + 1>
The loop index is <cfoutput>#CountVar#</cfoutput>.<br>
</cfloop>
<cfoutput>----------------第四章作业题:--------开始-----<br /></cfoutput>
<!--- 格式化日期字符串--->
<cfoutput>现在北京时间:#dateformat(now()<!---,"dddd,mmmm yyyy"--->)#<br /></cfoutput>
<!--- 根据系统小时得出是一天的什么时候--->
<cfset currentHour=#Hour(Now())# />
<cfif currentHour Lt 6 and #currentHour# Gte 0>
<cfoutput>当前为凌晨:#currentHour#<br /></cfoutput>
<cfelseif currentHour Lt 11 and #currentHour# Gte 6>
<cfoutput>当前为上午:#currentHour#<br /></cfoutput>
<cfelseif currentHour Lt 14 and #currentHour# Gte 11>
<cfoutput>当前为中午:#currentHour#<br /></cfoutput>
<cfelseif currentHour Lt 19 and #currentHour# Gte 14>
<cfoutput>当前为下午:#currentHour#<br /></cfoutput>
<cfelse>
<cfoutput>当前为晚上:#currentHour#<br /></cfoutput>
</cfif>
<!---打印九九乘法表--->
<cfoutput>打印九九乘法表方法一.<br /></cfoutput>
<cfloop index="loopCount2" from=1 to =9>
<cfset varTemp=0 />
<cfloop index="loopCount3" from=1 to =#loopCount2#>
<cfset varTemp=loopCount2*loopCount3>
<cfoutput>#loopCount3#*#loopCount2#=#varTemp#</cfoutput>
<cfset loopCount3=loopCount3+1 />
</cfloop>
<cfoutput>.<br /></cfoutput>
<cfset loopCount2=loopCount2+1 />
</cfloop>
<cfoutput>打印九九乘法表方法二.<br></cfoutput>
<cfset loopCount4=0 />
<cfloop condition="(loopCount4 Gte 0) and (loopCount4 Lte 9)">
<cfset loopCount4=loopCount4+1 />
<cfset varTemp2=0 />
<cfset loopCount5=0 />
<cfloop condition="(loopCount5 Gte 0) and (loopCount5 Lt loopCount4)">
<cfif loopCount4 eq 10>
<cfbreak>
</cfif>
<cfset loopCount5=loopCount5+1 />
<cfset varTemp2=loopCount4*loopCount5 />
<cfoutput>#loopCount5#*#loopCount4#=#varTemp2#</cfoutput>
</cfloop>
<cfoutput><br /></cfoutput>
</cfloop>
<cfoutput>----------------第四章作业题:--------结束-----<br /></cfoutput>
<!--- 在软岛科技技术培训资料中 &表示连接字符 下面这一句话所要表达的意思是把var1的值赋给var4,但<!--- &var2--->这一句话没有看懂 --->
<cfset var4=var1<!--- &var2 --->>
<cfoutput>#var4#<br /></cfoutput>
<!--- 特殊字符",',# 在这里不知道coldfusion是怎样编译CFML语言的
<cfset myString="We all said""for he's a jolly good fellow.""">
<cfset myString2="then we said "for she's a jolly good fellow"">
<cfoutput>#myString#<br />
<cfoutput>#myString2#<br />
<cfoutput>here is a proud sign:##</cfoutput>
--->
<cfoutput>----------------从DB查询数据并在应用程序页面显示-----<br /></cfoutput>
<cfquery datasource="tangfuqiang" name="CFTESTDB">
Select * from userInfo
</cfquery>
<!--- 建立一个HTML表格以显示Query --->
<Table cellpadding = 1 cellspacing = 1>
<TR>
<TD colspan = 2 bgColor = f0f0f0><B>编号</B></TD>
<TD bgColor = f0f0f0><B>姓名</B></TD>
<TD bgColor = f0f0f0><B>密码</B></TD>
<TD bgColor = f0f0f0><B>性别</B></TD>
<TD bgColor = f0f0f0><B>邮箱</B></TD>
<TD bgColor = f0f0f0><B>出生日期</B></TD>
<TD bgColor = f0f0f0><B>备注</B></TD>
</TR>
<!--- 输出Query并定义startrow及maxrows参数值. 使用Query变量
CurrentCount去追踪要显示的资料列. --->
<!---获取总的记录数--->
<cfset usercount=CFTESTDB.RecordCount />
<CFOUTPUT Query="CFTESTDB">
<TR>
<TD valign = top><font size = "-1">#user_Id#</font></TD>
<TD valign = top><font size = "-1">#user_Name#</font></TD>
<TD valign = top><font size = "-1">#user_Pwd#</font></TD>
<!--- 把从DB查询出来的性别转化为男、女--->
<cfif #user_Sex# eq 1>
<TD valign = top><font size = "-1">男</font></TD>
<cfelse>
<TD valign = top><font size = "-1">女</font></TD>
</cfif>
<TD valign = top><font size = "-1">#user_Mail#</font></TD>
<TD valign = top><font size = "-1">#user_Birthday#</font></TD>
<TD valign = top><font size = "-1">#user_Remark#</font></TD>
</TR>
</CFOUTPUT>
</Table>
<cfoutput>----------把记录插入库------开始----------<br /></cfoutput>
<cfform action="demolget.cfm" method="post">
<table>
<tr>
<td>姓名:<cfinput type="text" required="yes" name="userName" /></td>
<td>密码:<cfinput type="password" required="yes" name="userPwd" /></td>
</tr>
<tr>
<td>出生日期:<cfinput type="datefield" required="yes" validate="Date" name="userBirthday" /></td>
<td>男:<cfinput type="radio" name="userSex" required="yes" value="1" checked/>
女:<cfinput type="radio" name="userSex" required="yes" value="0" /></td>
</tr>
<tr>
<td>邮箱:<cfinput type="text" required="yes" name="userMail" /></td>
<td>备注:<cfinput type="text" required="yes" name="userRemark" /></td>
</tr>
<tr>
<td><cfinput type="submit" Name="insert" value="提交" /></td>
<td><cfinput type="reset" name="reset" value="重置" /></td>
</tr>
</table>
</cfform>
</body>
</html>
demolget.cfm文件如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<cfif isdefined("form.username")>
<cfif #ParameterExists(Form.insert)# or isdefined("form.insert")>
<cfif Not #LEN(Form.userName)# OR Not #LEN(Form.userPwd)# OR Not #LEN(Form.userSex)# OR Not #LEN(Form.userBirthday)#
OR Not #LEN(Form.userMail)# OR Not #LEN(Form.userRemark)#>
<cflocation url="demo2.cfm" />
</cfif>
<cfquery name="CFTESTDB" datasource="tangfuqiang">
select * from userInfo where user_Name='#Form.userName#'
</cfquery>
<!---<cfdump var="#CFTESTDB#"> --->
<cfif #CFTESTDB.recordCount# Gt 0>
<cfoutput><center>员工#Form.userName#已经存在了,请重新输入!</center></cfoutput>
<cfelse>
<cfquery name="CFTESTDB" datasource="tangfuqiang">
insert into userInfo(user_Name,user_Pwd,user_Birthday,user_Sex,user_Mail,user_Remark)
values('#Form.userName#','#Form.userPwd#','#Form.userBirthday#',#Form.userSex#,'#Form.userMail#','#Form.userRemark#')
</cfquery>
</cfif>
</cfif>
</cfif>
<body>
<cfquery datasource="tangfuqiang" name="CFTESTDB">
Select * from userInfo
</cfquery>
<!--- 建立一个HTML表格以显示Query --->
<Table cellpadding = 1 cellspacing = 1>
<TR>
<TD colspan = 2 bgColor = f0f0f0><B>编号</B></TD>
<TD bgColor = f0f0f0><B>姓名</B></TD>
<TD bgColor = f0f0f0><B>密码</B></TD>
<TD bgColor = f0f0f0><B>性别</B></TD>
<TD bgColor = f0f0f0><B>邮箱</B></TD>
<TD bgColor = f0f0f0><B>出生日期</B></TD>
<TD bgColor = f0f0f0><B>备注</B></TD>
</TR>
<!--- 输出Query并定义startrow及maxrows参数值. 使用Query变量
CurrentCount去追踪要显示的资料列. --->
<!---获取总的记录数--->
<cfset usercount=CFTESTDB.RecordCount />
<CFOUTPUT Query="CFTESTDB">
<TR>
<TD valign = top><font size = "-1">#user_Id#</font></TD>
<TD valign = top><font size = "-1">#user_Name#</font></TD>
<TD valign = top><font size = "-1">#user_Pwd#</font></TD>
<!--- 把从DB查询出来的性别转化为男、女--->
<cfif #user_Sex# eq 1>
<TD valign = top><font size = "-1">男</font></TD>
<cfelse>
<TD valign = top><font size = "-1">女</font></TD>
</cfif>
<TD valign = top><font size = "-1">#user_Mail#</font></TD>
<TD valign = top><font size = "-1">#user_Birthday#</font></TD>
<TD valign = top><font size = "-1">#user_Remark#</font></TD>
</TR>
</CFOUTPUT>
</Table>
</body>
</html>