CFtable: How to build a HTML table in ColdFusion

Introduction

The <cftable> tag in Adobe ColdFusion provides a straightforward way to create HTML tables dynamically based on query results. This example demonstrates how to use <cftable> and <cfcol> tags in ColdFusion to generate an HTML table displaying author information from a database. This approach can be incredibly useful for web applications needing to display database-driven content in a structured and readable format. The example also includes configurations like column headers, borders, and maximum rows, showcasing how ColdFusion makes building HTML tables easy and efficient.

This tutorial will break down each section of the code, explaining how each part contributes to constructing an HTML table. We’ll cover the <cfquery> tag, the <cftable> tag with its various attributes, and the <cfcol> tag for defining individual columns. Finally, we’ll summarize the example's utility and how it can be adapted to different datasets or design requirements.

Setting Up the Query with <cfquery>

The first step in this example is retrieving data from a database. Here, the <cfquery> tag is used to create a query named qAuthors, which retrieves the AuthorID, FirstName, and LastName columns from the AUTHORS table in the cfbookclub data source. The query results will later be used by the <cftable> tag to populate the HTML table with rows of author information.

The <cfquery> tag connects to the specified data source and executes the SQL command provided, which makes it a fundamental part of any ColdFusion-based data-driven page. Once the query runs, the resulting data set is stored in the qAuthors variable, making it available for use within the <cftable> tag.

Constructing the Table with <cftable>

The main feature of this example is the <cftable> tag, which creates the HTML table structure. The query attribute specifies the data source for the table, which in this case is the qAuthors query from earlier. By setting htmltable="yes", the table is rendered as an HTML table in the browser, enabling customization and easy viewing.

Several attributes control the appearance and functionality of the table. Setting border="yes" adds visible borders around the table cells, and colheaders="yes" ensures that column headers are included at the top of the table. The maxrows="8" and startrow="1" attributes limit the number of rows displayed and set the starting row, allowing for pagination if needed. Finally, colspacing="1" and headerlines="1" control the spacing between columns and the number of header lines, respectively, enhancing the table's readability.

Defining Columns with <cfcol>

Inside the <cftable> tag, individual columns are defined using the <cfcol> tag. Each <cfcol> tag represents one column in the table, with attributes specifying column headers, alignment, and the data to display. Here, three <cfcol> tags define columns for "Author ID," "First Name," and "Last Name," which display the values from the corresponding fields in the qAuthors query.

The header attribute in each <cfcol> tag specifies the column name, such as "Author ID" for the first column. The align attribute aligns text to the left, and the text attribute defines what data to display, referencing the query fields (#AuthorID#, #FirstName#, and #LastName#). This approach allows you to tailor each column individually, making it easy to manage how data appears in each column.

Summary

This example demonstrates how ColdFusion's <cftable> and <cfcol> tags make it simple to build HTML tables dynamically based on query results. By using these tags, you can create customizable, database-driven tables with minimal code, perfect for displaying information in a web-friendly format. The attributes in <cftable> and <cfcol> provide a range of options for styling and organizing data, allowing developers to tailor tables to their specific needs.

Whether you’re working with small datasets or large records, ColdFusion’s table-building capabilities can streamline your code and improve your workflow, making it a valuable tool for any web developer working with ColdFusion.


cftableHTMLTable.cfm

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cftable tag example: how to build a table in coldfusion (HTML Table)</title>
</head>

<body>
<h2 style="color:Crimson">cftable Tag Example: HTML Table</h2>

<cfquery name="qAuthors" datasource="cfbookclub">
 SELECT AuthorID, FirstName, LastName FROM AUTHORS
</cfquery>

<cftable 
 query="qAuthors" 
    border="yes" 
    maxrows="8" 
    startrow="1" 
    colspacing="1" 
    colheaders="yes"
    htmltable="yes" 
    headerlines="1"
    >
 <cfcol header="Author ID" align="left" text="#AuthorID#">
 <cfcol header="First Name" align="left" text="#FirstName#">
 <cfcol header="Last Name" align="left" text="#LastName#">
</cftable>
</body>
</html>





More ColdFusion examples

ColdFusion - How to get output of a query (defined start rows)

cfoutput - get output of a query (defined start rows)

cfoutputStartRow.cfm



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfoutput tag example: how to get output of a query (defined start rows) in coldfusion</title>
</head>

<body>
<h2 style="color:Crimson">cfoutput Tag Example: Query [startrow]</h2>

<cfquery name="qEmployees" datasource="cfdocexamples">
 SELECT Emp_ID, LastName, FirstName, Phone FROM EMPLOYEES
</cfquery>

<table cellpadding="0" cellspacing="0"  bordercolor="DodgerBlue" border="1">
 <tr bgcolor="DodgerBlue" style="color:White">
     <td><b>Emp ID</b></td>
     <td><b>Last Name</b></td>
     <td><b>First Name</b></td>
     <td><b>Phone</b></td>
    </tr>
 <cfoutput query="qEmployees" maxrows="5" startrow="5">
        <tr>
            <td>#Emp_ID#</td>
            <td>#LastName#</td>
            <td>#FirstName#</td>
            <td>#Phone#</td>
        </tr>
    </cfoutput>
</table>
</body>
</html>







More ColdFusion examples

ColdFusion - How to get output of a query

cfoutput - get output of a query

cfoutputQuery.cfm


<!DOCTYPE html">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfoutput tag example: how to get output of a query in coldfusion</title>
</head>

<body>
<h2 style="color:Crimson">cfoutput Tag Example: Query</h2>

<cfquery name="qEmployee" datasource="cfdocexamples">
 SELECT Emp_ID, FirstName, LastName, Salary FROM EMPLOYEE
</cfquery>

<table bgcolor="CadetBlue" cellpadding="0" cellspacing="0">
 <tr bgcolor="DarkCyan">
     <td><b>Emp ID</b></td>
     <td><b>First Name</b></td>
     <td><b>Last Name</b></td>
     <td><b>Salary</b></td>
    </tr>
 <cfoutput query="qEmployee">
        <tr>
            <td>#Emp_ID#</td>
            <td>#FirstName#</td>
            <td>#LastName#</td>
            <td>#Salary#</td>
        </tr>
    </cfoutput>
</table>
</body>
</html>





More ColdFusion examples

How to get output of a function in ColdFusion

cfoutput - get output of a function

cfoutputFunction.cfm



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfoutput tag example: how to get output of a function in coldfusion</title>
</head>

<body>
<h2 style="color:Crimson">cfoutput Tag Example: Function</h2>

<cfoutput>
 <b>Now:</b> #Now()#<br />
 <b>Today:</b> #DateFormat(Now())#<br />
 <b>Month:</b> #MonthAsString(Month(Now()))#<br />
 <b>Year:</b> #Year(Now())#<br />
</cfoutput>

</body>
</html>







More ColdFusion examples

How to use cfapplication tag in ColdFusion

cfapplication - simple use

Test.cfm



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfapplication tag example: simple use of cfapplication tag</title>
</head>

<body>
<h2 style="color:Crimson">cfapplication Tag Example</h2>

<cfdump var="#Application#">
<br />

<cfquery name="qAuthors" datasource="#Application.DSName#">
 SELECT COUNT(AuthorID) AS TotalAuthor FROM AUTHORS
</cfquery>
<cfdump var="#qAuthors#" label="qAuthors">
</body>
</html>


application.cfm


<cfapplication 
 name="cfexample"
    clientmanagement="yes" 
    sessionmanagement="yes" 
    sessiontimeout="#CreateTimeSpan(0,0,30,0)#"
    >

<cfset Application.DSName="cfbookclub">
<cfset Application.Description="ColdFusion Example">







More ColdFusion examples

How to use cfswitch with cfcase in ColdFusion

cfswitch with cfcase

cfswitch.cfm


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfswitch tag example: how to use cfswitch tag with cfcase in coldfusion</title>
</head>

<body>
<h2 style="color:Crimson">cfswitch Tag Example</h2>

<cfquery name="qAuthor" datasource="cfbookclub">
    SELECT AuthorID, FirstName, LastName FROM AUTHORS
</cfquery>

<cfset Theme="Pink">
<cfswitch expression="#Theme#">
 <cfcase value="Pink">
  <cfset HeaderRowColor="Crimson">
        <cfset RegularRowColor="Pink">
        <cfset AlternetRowColor="HotPink">
    </cfcase>
 <cfcase value="Green">
  <cfset HeaderRowColor="Green">
        <cfset RegularRowColor="SeaGreen">
        <cfset AlternetRowColor="LightGreen">
    </cfcase>
    <cfdefaultcase>
  <cfset HeaderRowColor="White">
        <cfset RegularRowColor="White">
        <cfset AlternetRowColor="White">
    </cfdefaultcase>
</cfswitch>

<cfoutput>
    <table cellpadding="0" cellspacing="0">
        <tr bgcolor=#HeaderRowColor#>
            <td><b>AuthorID</b></td>
            <td><b>FirstName</b></td>
            <td><b>LastName</b></td>
        </tr>
        <cfloop query="qAuthor">
            <tr bgcolor=#IIF(qAuthor.CurrentRow mod 2 eq 0,DE(RegularRowColor),DE(AlternetRowColor))#>
                <td>#AuthorID#</td>
                <td>#FirstName#</td>
                <td>#LastName#</td>
            </tr>
        </cfloop>
    </table>
</cfoutput>

</body>
</html>





More ColdFusion examples

ColdFusion - How to loop over a time range

cfloop - loop over a time range

cflooptime.cfm



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfloop tag example: how to loop over a time range in coldfusion</title>
</head>

<body>
<h2 style="color:Crimson">cfloop Tag Example: time range</h2>

<cfset LoopStartTime=CreateTime(0,0,0)>
<cfset LoopEndTime=CreateTime(5,0,0)>

<cfoutput>
 <b>LoopStartTime:</b> #LoopStartTime#<br />
 <b>LoopEndTime:</b> #LoopEndTime#<br />
</cfoutput>
<br /><br />

Loop start[step 30 minutes] ...<br />

<cfloop from="#LoopStartTime#" to="#LoopEndTime#" index="i" step="#CreateTimeSpan(0,0,30,0)#">
 <cfoutput>
     <b>Current Position:</b> #TimeFormat(i)#<br />
    </cfoutput>
</cfloop>

</body>
</html>







More ColdFusion examples

How to loop over a query in ColdFusion

cfloop - loop over a query

cfloopquery.cfm


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfloop tag example: how to loop over a query in coldfusion</title>
</head>

<body>
<h2 style="color:Crimson">cfloop Tag Example: Query</h2>

<cfquery name="qEmployees" datasource="cfdocexamples">
 SELECT Emp_ID, FirstName, LastName FROM EMPLOYEES
</cfquery>

<cfoutput>
    <table cellpadding="0" cellspacing="0" style="color:White" border="1">
        <tr bgcolor="HotPink">
            <td><b>Emp_ID</b></td>
            <td><b>FirstName</b></td>
            <td><b>LastName</b></td>
        </tr>
        <cfloop query="qEmployees" startrow="1" endrow="10">
            <tr bgcolor=#IIF(qEmployees.CurrentRow mod 2 eq 0,DE("IndianRed"),DE("Red"))#>
                <td>#Emp_ID#</td>
                <td>#FirstName#</td>
                <td>#LastName#</td>
            </tr>
        </cfloop>
    </table>
</cfoutput>

</body>
</html>







More ColdFusion examples

How to loop over a list in ColdFusion

cfloop - loop over a list

cflooplist.cfm



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfloop tag example: how to loop over a list in coldfusion</title>
</head>

<body>
<h2 style="color:Crimson">cfloop Tag Example: List</h2>

<cfloop list="Jenny,Jones,John,Ben,Raymond,Kak" index="ListElement" delimiters=",">
 <cfoutput>
  <b>Current List Element:</b> #ListElement#<br />
 </cfoutput>
</cfloop>

<br /><br />
<cfset CityList="Rome;London;NewYork">
<cfoutput>
 <b>CityList:</b> #CityList#<br />
</cfoutput>

<cfloop list="#CityList#" index="ListElement" delimiters=";">
 <cfoutput>
  <b>Current List Element:</b> #ListElement#<br />
 </cfoutput>
</cfloop>

</body>
</html>







More ColdFusion examples

How to perform index loop in ColdFusion

cfloop - index loop

cfloopindex.cfm



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfloop tag example: how to use index loop in coldfusion</title>
</head>

<body>
<h2 style="color:Crimson">cfloop Tag Example: index</h2>

<cfoutput>
    <b>Loop start[1 to 10]....</b><br />
    <cfloop from="1" to="10" index="Counter">
        Current position: #Counter#<br />
    </cfloop>
    <b>Loop End....</b><br />

 <br /><br />
    
    <b>Loop start[1 to 10; step 2]....</b><br />
    <cfloop from="1" to="10" step="2" index="Counter">
        Current position: #Counter#<br />
    </cfloop>
    <b>Loop End....</b><br />
    
</cfoutput>
</body>
</html>







More ColdFusion examples

How to loop over a date range in ColdFusion

cfloop - loop over a date range

cfloopdate.cfm



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfloop tag example: how to loop over a date range in coldfusion</title>
</head>

<body>
<h2 style="color:Crimson">cfloop Tag Example: date range</h2>

<cfset LoopStartDate="5-Dec-2008">
<cfset LoopEndDate="12-Dec-2008">

<cfoutput>
 <b>LoopStartDate:</b> #LoopStartDate#<br />
 <b>LoopEndDate:</b> #LoopEndDate#<br />
</cfoutput>
<br /><br />

Loop start ...<br />

<cfloop from="#LoopStartDate#" to="#LoopEndDate#" index="i">
 <cfoutput>
     <b>Current Position:</b> #DateFormat(i)#<br />
    </cfoutput>
</cfloop>

<br /><br />
Loop start[step 2 days] ...<br />
<cfloop from="#LoopStartDate#" to="#LoopEndDate#" index="i" step="#CreateTimeSpan(2,0,0,0)#">
 <cfoutput>
     <b>Current Position:</b> #DateFormat(i)#<br />
    </cfoutput>
</cfloop>
</body>
</html>







More ColdFusion examples

How to perform conditional loop in ColdFusion

cfloop - conditional loop

cfloopconditional.cfm



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfloop tag example: how to use conditional loop in coldfusion</title>
</head>

<body>
<h2 style="color:Crimson">cfloop Tag Example: conditional</h2>
<cfset LoopEndPosition=5>
<cfoutput>
 <b>Loop End Position:</b> #LoopEndPosition#
</cfoutput>
<br /><br />

<cfset Counter=0>
Loop Start...<br />
<cfloop condition="LoopEndPosition GT Counter">
 <cfset Counter=Counter+1>
    <cfoutput>
        <b>Current Position:</b> #Counter#<br />
    </cfoutput>
</cfloop>
Loop End...

</body>
</html>







More ColdFusion examples

How to loop over an array in ColdFusion

cfloop - loop over an array

cflooparray.cfm


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfloop tag example: how to loop over an array in coldfusion</title>
</head>

<body>
<h2 style="color:Crimson">cfloop Tag Example: Array</h2>

<cfset ColorArray=ArrayNew(1)>
<cfset ColorArray[1]="Red">
<cfset ColorArray[2]="Green">
<cfset ColorArray[3]="Yellow">
<cfset ColorArray[4]="Blue">

<cfdump var="#ColorArray#" label="ColorArray">
<br />

Loop Start[ColorArray]...<br />
<cfloop array="#ColorArray#" Index="Color">
 <cfoutput>
  <b>Current Color:</b> #Color#<br />
 </cfoutput>
</cfloop>
</body>
</html>





More ColdFusion examples

CFfunction - How to create a function in ColdFusion

cffunction - create a function

cffunction.cfm


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cffunction tag example: how to create a function in coldfusion</title>
</head>

<body>
<h2 style="color:Crimson">cffunction Tag Example</h2>

<cfset TotalRecord=GetTotalRecord("cfbookclub", "Books", "BookID")>
<cfoutput>
 <b>Total Record [DataSource cfbookclub; Table BOOKS]: </b> #TotalRecord#
</cfoutput>
<cffunction name="GetTotalRecord" access="remote" returntype="numeric" description="Return table number of records">
 <cfargument name="DataSource" type="string" required="yes">
 <cfargument name="TableName" type="string" required="yes">
    <cfargument name="FieldName" type="string" required="yes">
 <cfquery name="qTableData" datasource="#arguments.DataSource#">
     SELECT COUNT(#arguments.FieldName#) AS TotalRecord FROM #arguments.TableName#
    </cfquery>
    <cfreturn qTableData.TotalRecord>
</cffunction>
</body>
</html>





More ColdFusion examples

How to use cfcase in cfswitch in ColdFusion

cfcase in cfswitch

cfcase.cfm


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfcase tag example: how to use cfcase tag in cfswitch tag</title>
</head>

<body>
<h2 style="color:Crimson">cfcase Tag Example</h2>


<cfset Color="DarkBlue">
<cfoutput>
<b>Your Color:</b> #Color#
<br />
</cfoutput>

<cfswitch expression="#Color#">
 <cfcase value="Crimson">
  You choose color: Crimson[#DC143C]
    </cfcase>
 <cfcase value="DarkBlue">
  You choose color: DarkBlue[#00008B]
    </cfcase>
 <cfcase value="DarkRed">
  You choose color: DarkRed[#8B0000]
    </cfcase>
 <cfcase value="Lime">
  You choose color: Lime[#00FF00]
    </cfcase>
    <cfdefaultcase>
  You choose unknown color
    </cfdefaultcase>
</cfswitch>

</body>
</html>





More coldfusion examples

ColdFusion - Put a flash format calendar in a form

cfcalendar - put an interactive flash format calendar in a form

cfcalendar.cfm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfcalendar tag example: how to put an interactive flash format calendar in a form</title>
</head>

<body>
<h2 style="color:Crimson">cfcalendar Tag Example</h2>

<cfif IsDefined("Form.SubmitDate")>
 <cfoutput>
     <cfif Form.ArraivalDate LTE Now()>
         Select a date after: <b>#DateFormat(Now())#</b>
  <cfelse>
   Your Arraival Date Is: <b>#DateFormat(Form.ArraivalDate)#</b>
        </cfif>
    </cfoutput>
</cfif>
<br /><br />

<cfform name="TestForm" format="flash" action="" height="350" width="400" skin="halogreen">
 <cfcalendar name="ArraivalDate" required="yes">
    <cfinput type="submit" name="SubmitDate" value="Submit Arrival Date">
</cfform>

</body>
</html>





More ColdFusion examples

How to use cfbreak to break a loop in ColdFusion

cfbreak - break a loop

cfbreak.cfm



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfbreak tag example: how to use cfbreak tag to break a loop</title>
</head>

<body>
<h2 style="color:Crimson">cfbreak Tag Example</h2>

<cfloop from="1" to="10" index="Counter" >
 <cfoutput>
  <b>Current Counter:</b> #Counter#
        <br />
        <cfif Counter EQ 6>
         loop break at the position 6
            <cfbreak>
        </cfif>
 </cfoutput>
</cfloop>

<br /><br />
Other code goes here...

</body>
</html>







More ColdFusion examples

How to use cfargument inside cffunction in ColdFusion

cfargument in cffunction

cfargument.cfm


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfargument tag example: how to use cfargument tag in cffunction tag</title>
</head>

<body>
<h2 style="color:Crimson">cfargument Tag Example</h2>

<cfoutput>
 <b>Book Title [BookID 1]:</b> #GetBookTitleByID(1)#
 <br />
 <b>Book Title [BookID 10]:</b> #GetBookTitleByID(10)#
    <br />
 <b>Book Title [BookID 15]:</b> #GetBookTitleByID(15)#
    <br />
 <b>Book Title [BookID 20]:</b> #GetBookTitleByID(20)#
</cfoutput>

<cffunction name="GetBookTitleByID" access="remote" returntype="string" description="Return Book Title by ID">
    <cfargument name="BookID" type="numeric" required="yes">
 <cfquery name="qBook" datasource="cfbookclub">
     SELECT Title FROM BOOKS
        WHERE BookID=#arguments.BookID#
    </cfquery>
    <cfreturn qBook.Title>
</cffunction>
</body>
</html>






More ColdFusion examples

CFabort - How to stop processing of a coldfusion page at the tag location

cfabort - stop the processing of a coldfusion page at the tag location

cfabort.cfm


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>cfabort tag example: how to stop the processing of a coldfusion page at the tag location</title>
</head>

<body>
<h2 style="color:Crimson">cfabort Tag Example</h2>

<cfloop from="1" to="25" index="LoopCounter" >
 <cfoutput>
  <b>Current LoopCounter:</b> #LoopCounter#
        <br />
        <cfif LoopCounter EQ 5>
         <cfabort showerror="cfabort call at the position 5">
        </cfif>
 </cfoutput>
</cfloop>

</body>
</html>





More ColdFusion examples

How to set a variable with name and value in ColdFusion

SetVariable() - set a variable with name and value

SetVariable.cfm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>SetVariable function example: how to set a variable with name and value</title>
</head>

<body>
<h2 style="color:Crimson">SetVariable Function Example</h2>

<cfset MyID=5>
<cfset Setvariable("ID" & MyID,"Jones" )>
<cfset SetVariable("City","Rome")>
<cfoutput>
 <b>ID5:</b> #ID5#
 <br />
 <b>City:</b> #City#
</cfoutput>

</body>
</html>






More ColdFusion examples

ColdFusion - Evaluate one or more string expressions dynamically

Evaluate() - evaluate one or more string expressions dynamically from left to right

Evaluate.cfm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Evaluate function example: how to evaluate one or more string expressions dynamically from left to right</title>
</head>

<body>
<h2 style="color:Crimson">Evaluate Function Example</h2>
<cfoutput>
<b>Result of expression[(10+5)*5]:</b> #Evaluate((10+5)*5)#
<br />
<b>Result of expression[(10+5+5)*5]:</b> #Evaluate((10+5+5)*5)#
<br />
<b>Result of expression[(10+5)/5]:</b> #Evaluate((10+5)/5)#
</cfoutput>
</body>
</html>






More ColdFusion examples

ColdFusion - Escape any double-quotation marks in the parameter and wraps the result in double-quotation marks

DE() - escape any double-quotation marks in the parameter and wraps the result in double-quotation marks

DE.cfm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DE function example: how to escape any double-quotation marks in the parameter and wraps the result in double-quotation marks</title>
</head>

<body>
<h2 style="color:Crimson">DE Function Example</h2>

<cfquery name="qBook" datasource="cfbookclub">
 SELECT BookID, Title FROM BOOKS
</cfquery>

<table cellpadding="0" cellspacing="0">
 <tr bgcolor="Green" align="center">
     <td><b>BookID</b></td>
        <td><b>Title</b></td>
    </tr>
    <cfloop query="qBook">
  <cfoutput>
            <tr bgcolor="#IIF(qBook.CurrentRow mod 2 eq 0, DE("Pink"),DE("HotPink"))#">
                <td>#BookID#</td>
                <td>#Title#</td>
            </tr>
        </cfoutput>    
    </cfloop>
</table>
</body>
</html>






More ColdFusion examples

How to get meta data for a cfc (component) in ColdFusion

GetComponentMetaData() - get metadata for a cfc

GetComponentMetaData.cfm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GetComponentMetaData function example: how to get metadata for a cfc (coldfusion component)</title>
</head>

<body>
<h2 style="color:Crimson">GetComponentMetaData Function Example</h2>

<cfset MyComponentMetaData=GetComponentMetaData("MyComponent")>

<cfdump var="#MyComponentMetaData#" label="MyComponent.cfc MetaData" >
</body>
</html>


MyComponent.cfc


<cfcomponent output="no" Author="Jones" displayname="Math">
 <cffunction name="Sum" access="remote" returntype="numeric" description="Sum two numbers">
  <cfargument name="Value1" type="numeric" required="yes">
  <cfargument name="Value2" type="numeric" required="yes">
  <cfset Sum=arguments.value1+arguments.value2>
  <cfreturn Sum>
 </cffunction>
</cfcomponent>






More ColdFusion examples

CreateObject() - How to create a ColdFusion object (type component)

CreateObject() - create a coldfusion object (type component)

CreateObject.cfm



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CreateObject function example: how to create a coldfusion object (type component)</title>
</head>

<body>
<h2 style="color:Crimson">CreateObject Function Example: Component</h2>

<cfset MyObject=CreateObject("component","MyComponent")>

<cfoutput>
 <b>MyObject.Sum(10,25):</b> #MyObject.Sum(10,25)#
    <br /><br />
</cfoutput>
<cfdump var="#GetMetaData(MyObject)#" label="MyObject" >
</body>
</html>


MyComponent.cfc


<cfcomponent output="no" Author="Jones" displayname="Math">
 <cffunction name="Sum" access="remote" returntype="numeric" description="Sum two numbers">
  <cfargument name="Value1" type="numeric" required="yes">
  <cfargument name="Value2" type="numeric" required="yes">
  <cfset Sum=arguments.value1+arguments.value2>
  <cfreturn Sum>
 </cffunction>
</cfcomponent>







More ColdFusion examples

ColdFusion - How to determine whether a user is logged in

IsUserLoggedIn() - determine whether a user is logged in

IsUserLoggedIn.cfm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IsUserLoggedIn function example: how to determine whether a user is logged in</title>
</head>

<body>
<h2 style="color:Crimson">IsUserLoggedIn Function Example</h2>


<cfoutput>
 <cfif IsUserLoggedIn()>
        User is logged in.<br />
        <b>User Name:</b> #GetAuthUser()#
    <cfelse>
  User is not logged in
    </cfif>
</cfoutput>

<cflogout>

<br /><br />
-----After logged out---
<br /><br />

<cfoutput>
 <cfif IsUserLoggedIn()>
        User is logged in.<br />
        <b>User Name:</b> #GetAuthUser()#
    <cfelse>
  User is not logged in
    </cfif>
</cfoutput>

</body>
</html>






More ColdFusion examples

ColdFusion - Determine whether an authenticated user belongs to the specified role

IsUserInRole() - determine whether an authenticated user belongs to the specified role

IsUserInRole.cfm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IsUserInRole function example: how to determine whether an authenticated user belongs to the specified role</title>
</head>

<body>
<h2 style="color:Crimson">IsUserInRole Function Example</h2>

<cfset UserRoles=GetUserRoles()>

<cfoutput>
 <cfif IsUserInRole("admin")>
        <b>Current user is in role</b>: admin
    <cfelseif IsUserInRole("superadmin")>
        <b>Current user is in role</b>: superadmin
 <cfelse>
     <b>Current user is not in roles: admin, superadmin</b>        
    </cfif>
</cfoutput>
</body>
</html>






More ColdFusion example

ColdFusion IIF() - How to use IIF function

IIF() function

IIF.cfm


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>IIF function example: how to use IIF function in coldfusion</title>
</head>

<body>
<h2 style="color:Crimson">IIF Function Example</h2>
<cfset DefaultColor="HotPink">

<cfoutput>
<b>Default Color</b>:
#IIF(IsDefined("DefaultColor"),DE(DefaultColor),DE("Green") )#
</cfoutput>
</body>
</html>





More ColdFusion examples

ColdFusion Hash() function with MD5 algorithm

Hash() - MD5 algorithm

HashMD5.cfm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Hash function example: how to use Hash function with MD5 algorithm</title>
</head>

<body>
<h2 style="color:Crimson">Hash Function Example: MD5</h2>

<cfset TestString="This is a string">
<cfset HashTestString=Hash(TestString,"MD5")>
<cfoutput>
<b>TestString:</b> #TestString#
<br />
<b>Hash TestString[algorithm MD5]:</b> #HashTestString#
</cfoutput>
</body>
</html>





More ColdFusion example

How to generate a secret key with algorithm AES in ColdFusion

GenerateSecretKey() - generate a secret key with algorithm AES

GenerateSecretKeyAES.cfm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GenerateSecretKey function example: how to generate a secret key with algorithm AES</title>
</head>

<body>
<h2 style="color:Crimson">GenerateSecretKey Function Example: AES</h2>

<cfset SecretKey=GenerateSecretKey("AES")>
<cfoutput>
<b>SecretKey[algorith AES]:</b> #SecretKey#
</cfoutput>
</body>
</html>






More ColdFusion examples

How to encrypt a string using a specific algorithm and encoding method in ColdFusion

Encrypt() - encrypt a string using a specific algorithm and encoding method

Encrypt.cfm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Encrypt function example: how to encrypt a string using a specific algorithm and encoding method</title>
</head>

<body>
<h2 style="color:Crimson">Encrypt Function Example</h2>
<cfset TestString="This is a string">
<cfset SecretKey=GenerateSecretKey("DESEDE")>
<cfset EncryptString=Encrypt(TestString,SecretKey)>

<cfoutput>
<b>TestString:</b> #TestString#
<br />
<b>TestString[after encrypt]:</b> #EncryptString#
<br />
<b>TestString[after decrypt]:</b> #Decrypt(EncryptString,SecretKey)#
</cfoutput>
</body>
</html>





More ColdFusion examples

How to decrypt a string that is encrypted using a standard encryption technique in ColdFusion

Decrypt() - decrypt a string that is encrypted using a standard encryption technique

Decrypt.cfm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Decrypt function example: how to decrypt a string that is encrypted using a standard encryption technique</title>
</head>

<body>
<h2 style="color:Crimson">Decrypt Function Example</h2>
<cfset TestString="This is a string for test Decrypt function">
<cfset SecretKey=GenerateSecretKey("BLOWFISH")>
<cfset EncryptString=Encrypt(TestString,SecretKey)>

<cfoutput>
<b>TestString:</b> #TestString#
<br />
<b>TestString[after encrypt]:</b> #EncryptString#
<br />
<b>TestString[after decrypt]:</b> #Decrypt(EncryptString,SecretKey)#
</cfoutput>
</body>
</html>





More ColdFusion examples

ColdFusion CreateUUID() - How to create an Universally Unique Identifier (UUID)

CreateUUID() - create an Universally Unique Identifier (UUID)

CreateUUID.cfm



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CreateUUID function example: how to create an Universally Unique Identifier (UUID)</title>
</head>

<body>
<h2 style="color:Crimson">CreateUUID Function Example</h2>

<cfset UUID=CreateUUID()>
<cfoutput><b>Universally Unique Identifier: #UUID#</b></cfoutput>
</body>
</html>







More ColdFusion examples

ColdFusion ImageWrite() - How to write a coldfusion image to a file

ImageWrite() - write a coldfusion image to the specified filename or destination

ImageWrite.cfm


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ImageWrite function example: how to write a coldfusion image to the specified filename or destination</title>
</head>

<body>
<h2 style="color:Crimson">ImageWrite Function Example</h2>

<cfimage source="Test.jpg" name="TestImage">

<cfscript>
 ImageWrite(TestImage,"Image.jpg",1);
</cfscript>

<img src="Test.jpg"/>
<img src="Image.jpg"/>
</body>
</html>





How to draw a text string on a ColdFusion image

ImageDrawText() - draw a text string on a coldfusion image

ImageDrawText.cfm



<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ImageDrawText function example: how to draw a text string on a coldfusion image</title>
</head>

<body>
<h2 style="color:Crimson">ImageDrawText Function Example</h2>

<cfset TestImage=ImageNew("",200,200)>
<cfset ImageSetDrawingColor(TestImage,"red")>
<cfset ImageSetAntialiasing(TestImage,"on")>
<cfset ImageDrawText(TestImage,"Sowa Chan Pakhi",25,25)>
<cfimage action="writeToBrowser" source="#TestImage#">
</body>
</html>






How to crop a ColdFusion image to a specified rectangular area

ImageCrop() - crop a coldfusion image to a specified rectangular area

ImageCrop.cfm


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ImageCrop function example: how to crop a coldfusion image to a specified rectangular area</title>
</head>

<body>
<h2 style="color:Crimson">ImageCrop Function Example</h2>

<cfimage source="Test.jpg" name="TestImage">

<cfset ImageCrop(TestImage,250,10,250,300)>


<cfimage source="#TestImage#" action="write" destination="ModifiedTestImage.gif" overwrite="yes">

<img src="Test.jpg"/>
<img src="ModifiedTestImage.gif"/>
</body>
</html>





More ColdFusion example

ColdFusion - How to append text to the page output stream

WriteOutput() - append text to the page output stream

WriteOutput.cfm


<!DOCTYPE html">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>WriteOutput function example: how to append text to the page output stream</title>
</head>

<body>
<h2 style="color:Crimson">WriteOutput Function Example</h2>

<cfscript>
 Value1=10;
 value2=15;
 Value3=25;
 Sum=Value1+Value2+Value3;
 WriteOutput("<b>Value1</b>" & "=" & Value1 & "<br/>");
 WriteOutput("<b>Value2</b>" & "=" & Value2 & "<br/>");
 WriteOutput("<b>Value3</b>" & "=" & Value3 & "<br/>");
 WriteOutput("<b>Value1+Value2+Value3</b>= "& Sum);
</cfscript>
</body>
</html>






More ColdFusion examples

ColdFusion - How to create a temporary file in a directory

GetTempFile() - create a temporary file in a directory

GetTempFile.cfm


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GetTempFile function example: how to create a temporary file in a directory</title>
</head>

<body>
<h2 style="color:Crimson">GetTempFile Function Example</h2>

<cfoutput>
<b>Temporary Directory:</b> #GetTempDirectory()#
<br />
<b>Craete a Temporary File:</b> #GetTempFile(GetTempDirectory(),"TestTempFile")#
</cfoutput>
</body>
</html>






More ColdFusion examples

How to get the current ColdFusion page context object

GetPageContext() - get the current coldfusion page context object

GetPageContext.cfm


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GetPageContext function example: how to get the current coldfusion page context object</title>
</head>

<body>
<h2 style="color:Crimson">GetPageContext Function Example</h2>

<cfset CurrentPageContext=GetPageContext()>
<cfdump var="#CurrentPageContext#" label="Current Page Context">

</body>
</html>






More ColdFusion examples

ColdFusion - How to get server parformace metrics

GetMetricData() - get server parformace metrics

GetMetricData.cfm


<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GetMetricData function example: how to get server parformace metrics</title>
</head>

<body>
<h2 style="color:Crimson">GetMetricData Function Example</h2>

<cfset MetricDataPERF=GetMetricData("perf_monitor")>
<cfdump var="#MetricDataPERF#" label="Metric Data[perf_monitor]">

<cfset MetricDataSimpleLoad=GetMetricData("simple_load")>
<cfset MetricDataPreviousReqTime=GetMetricData("prev_req_time")>
<cfset MetricDataAvgReqTime=GetMetricData("avg_req_time")>


<cfoutput>
 <b>MetricData Simple Load[simple_load]:</b> #MetricDataSimpleLoad#
 <br />
 <b>MetricData Previuos Request Time[prev_req_time]:</b> #MetricDataPreviousReqTime#
    <br />
 <b>MetricData Avarage Request Time[avg_req_time]:</b> #MetricDataAvgReqTime#
</cfoutput>

</body>
</html>






More ColdFusion examples