Creating a File2Table Function - Exercise

Contact Us or call 1-877-932-8228
Creating a File2Table Function - Exercise

Creating a File2Table Function

Duration: 20 to 30 minutes.

In this exercise, you will modify RunningLog.cfm to include a function that creates a table from a tab-delimited text file.

  1. Open ReusingCode/Exercises/RunningLog.cfm in your editor.
  2. Create a function called File2Table that takes two arguments: the path to the file and an array of the table headers.
  3. Modify the page so that the function contains all the processing code that loads the file and creates and returns an HTML table.
  4. If the file cannot be opened, the function should return "File not found".
  5. the function and send the result to the browser.

Add formatting functionality to the function. For example, make the border size and the background color of the table controlled by the function call.

The function currently has the headers hardcoded in. This is not great for reuse. Change this so the function excepts a Headers array and loops through the array to output the headers.

Solution:

ReusingCode/Solutions/RunningLog.cfm
<html>
<head>
  <title>Running Log</title>
  <style type="text/css">
  	.grayBg {background-color:#cccccc; font-style:italic;}
	.whiteBg {background-color:#ffffff; font-weight:bold}
  </style>
</head>
<body>
<h1>Running Log</h1>
<a href="AddEntry.cfm">Add Entry</a><hr/>
<cfoutput>#File2Table("Logs/RunningLog.txt")#</cfoutput>
</body>
</html>
<cffunction name="File2Table" returntype="string">
	<cfargument name="PathToFile" type="string" required="yes">
	
	<cfset FullPath = ExpandPath(ARGUMENTS.PathToFile)>
	<cfset CrLf = chr(10) & chr(13)>
	<cfif FileExists(FullPath)>
		<cffile action="read" file="#FullPath#" variable="myfile">
		
		<cfsavecontent variable="Table">
			<cfoutput>
			<table border="1" bgcolor="##ccccff">
			<tr>
				<th>Date</th>
				<th>Distance</th>
				<th>Time</th>
				<th>Comments</th>
			</tr>
			<cfloop list="#myfile#" index="run" delimiters="#CrLf#">
				<cfif NOT isDefined("bg") OR bg EQ "grayBg">
					<cfset bg="whiteBg">
				<cfelse>
					<cfset bg="grayBg">
				</cfif>
			
				<tr class="#bg#">
				<cfloop list="#run#" index="col" delimiters="#chr(9)#">
					<td>#col#</td>
				</cfloop>
				</tr>
			</cfloop>
			</table>
			</cfoutput>
		</cfsavecontent>
		<cfreturn Table>
	<cfelse>
		<cfreturn "File not found.">
	</cfif>
</cffunction>
            

Challenge Solution:

ReusingCode/Solutions/RunningLog-challenge.cfm
---- C O D E   O M I T T E D ----
<cffunction name="File2Table" returntype="string">
	<cfargument name="PathToFile" type="string" required="yes">
	<cfargument name="OddColor" type="string" default="##ffffff">
	<cfargument name="EvenColor" type="string" default="##cccccc">
	
	<cfset FullPath = ExpandPath(ARGUMENTS.PathToFile)>
	<cfset CrLf = chr(10) & chr(13)>
	<cfif FileExists(FullPath)>
		<cffile action="read" file="#FullPath#" variable="myfile">
		
		<cfsavecontent variable="Table">			
			<cfoutput>
			<table border="1" bgcolor="##ccccff">
			<tr>
				<th>Date</th>
				<th>Distance</th>
				<th>Time</th>
				<th>Comments</th>
			</tr>
			<cfloop list="#myfile#" index="run" delimiters="#CrLf#">
				<cfif NOT isDefined("bg") OR bg EQ ARGUMENTS.EvenColor>
					<cfset bg=ARGUMENTS.OddColor>
				<cfelse>
					<cfset bg=ARGUMENTS.EvenColor>
				</cfif>

				<tr bgcolor="#bg#">
				<cfloop list="#run#" index="col" delimiters="#chr(9)#">
					<td>#col#</td>
				</cfloop>
				</tr>
			</cfloop>
			</table>
			</cfoutput>
		</cfsavecontent>
		<cfreturn Table>
	<cfelse>
		<cfreturn "File not found.">
	</cfif>
</cffunction>

Challenge Solution:

ReusingCode/Solutions/RunningLog-challenge2.cfm
---- C O D E   O M I T T E D ----
<cffunction name="File2Table" returntype="string">
	<cfargument name="PathToFile" type="string" required="yes">
	<cfargument name="Headers" type="array" required="yes">
	<cfargument name="OddColor" type="string" default="##ffffff">
	<cfargument name="EvenColor" type="string" default="##cccccc">
	
	<cfset FullPath = ExpandPath(ARGUMENTS.PathToFile)>
	<cfset CrLf = chr(10) & chr(13)>
	<cfif FileExists(FullPath)>
		<cffile action="read" file="#FullPath#" variable="myfile">
		
		<cfsavecontent variable="Table">			
			<cfoutput>
			<table border="1" bgcolor="##ccccff">
			<tr>
				<cfloop index="i" from="1" to="#ArrayLen(ARGUMENTS.Headers)#">
					<th>#ARGUMENTS.Headers[i]#</th>
				</cfloop>
			</tr>
---- C O D E   O M I T T E D ----
</cffunction>
Next