This home page displays the result of a database query, inserts code from an included file, reads text from a text file, and displays the contents of an application variable. You wouldn't use all of these techniques in a real application, but you can look them over and choose what you like. To do this, build a home page, create an appropriate database table, and build routines to write your messages. For this demo, name the home page homepage.cfm (a bland, but memorable filename).
DON'T USE ALL THESE TECHNIQUES AT ONCE! <hr> Database Query... <p> <cftry> <cfquery name="Seenotice" datasource="context"> select * from Notice </cfquery> <cfif Seenotice.recordcount> <cfoutput query="Seenotice">#Notice#</cfoutput> <cfelse> [The database has no message yet.] </cfif> <cfcatch type="any"> THE DATABASE IS UNDERGOING MAINTENANCE.<br> Regretfully, we ask you to try back later. </cfcatch> </cftry>
<hr> Include a File... <p> <cftry> <cfinclude template="notecfm.unk"> <cfcatch type="any"> [There is no message include file yet.] </cfcatch> </cftry>
<hr>
Read a File with CFFILE...
<p>
<cfset Fullname="c:\doc\notehtm.unk">
<cfif fileExists("#Fullname#")>
<cffile action="read" file="#Fullname#" variable="NoteText">
<cfoutput>#NoteText#</cfoutput>
<cfelse>
[There is no message text file yet.]
</cfif>
<hr>
Display an Application Variable...
<p>
<!--- Add the next line if the application
hasn't been defined elsewhere --->
<cfapplication name="Notice" sessionmanagement="no">
<cflock name="NoticeRead" timeout="30" type="readonly">
<cfparam name="application.notice" default="oops">
<cfset Appnotice=application.notice>
</cflock>
<cfif Appnotice is "oops">
<cfif fileExists("noticeapp.txt")>
<cffile action="read" file="noticeapp.txt"
variable="NoteApp">
<cfset Appnotice=NoteApp>
<cflock name="NoticeSet" timeout="30" type="exclusive">
<cfset application.notice=NoteApp>
</cflock>
<cfoutput>#NoteApp#</cfoutput>
<cfelse>
[There is no message file to load the variable,
hence no application message.]
</cfif>
<cfelse>
<cfoutput>#Appnotice#</cfoutput>
</cfif>
Read the notice from the database. If no notice exists, insert an almost empty notice - a null string would fail - and then add a row to the read query and populate the appropriate column (cell) so the form won't break: use a combination of queryAddRow and querySetCell to do this. If the form has been submitted, and the message isn't blank, change the database to match the form. When you code the textarea tag to accept your message, set wrap to physical so it preserves line breaks you might happen to use in your message. Once you've created this code, try it out; then browse homepage.cfm to see the result.
<!--- Uncomment to block remote users
<cfif cgi.remote_addr is not "127.0.0.1">
<cflocation url="http://www.cftipsplus.com">
</cfif>
--->
<cfquery name="Chknotice" datasource="context">
select * from Notice
</cfquery>
<cfif not Chknotice.recordcount>
<cfquery name="Blanknotice" datasource="context">
insert into Notice (Notice)
values (' ')
</cfquery>
<cfset dummy=QueryAddRow(Chknotice)>
</cfif>
<cfif isDefined("form.Doit") and len(trim(form.Notice))>
<cfquery name="setnotice" datasource="context">
update Notice set
Notice = '#trim(form.Notice)#'
</cfquery>
<cfset dummy=querySetCell(ChkNotice, "Notice", trim(form.Notice))>
</cfif>
<form name="Setnotice" method="post" action="noticedb.cfm">
<textarea name="Notice" wrap="physical" rows="5" cols="60">
<cfoutput>#trim(Chknotice.Notice)#</cfoutput></textarea>
<input type="Submit" name="Doit" value="Change">
</form>
<!--- Uncomment to block remote users
<cfif cgi.remote_addr is not "127.0.0.1">
<cflocation url="http://www.cftipsplus.com">
</cfif>
--->
<cfset Fullname="#getDirectoryFromPath(cf_template_path)#"&"notecfm.unk">
<cfif isDefined("form.Doit") and len(trim(form.Notice))>
<cffile action="write" file="#Fullname#" output="#form.Notice#" addnewline="Yes">
</cfif>
<cfif fileExists("#Fullname#")>
<cffile action="read" file="#Fullname#" variable="NoteText">
<cfelse>
[There is no message text file yet.]
</cfif>
<cfparam name="NoteText" default=" ">
<form name="Setnotice" method="post" action="noticein.cfm">
<textarea name="Notice" wrap="physical" rows="5" cols="60">
<cfoutput>#trim(NoteText)#</cfoutput></textarea>
<input type="Submit" name="Doit" value="Change">
</form>
<!--- Uncomment to block remote users
<cfif cgi.remote_addr is not "127.0.0.1">
<cflocation url="http://www.cftipsplus.com">
</cfif>
--->
<cfset Fullname="C:\doc\notehtm.unk">
<!--- <cfset Fullname="#getDirectoryFromPath(cf_template_path)#"&"notehtm.unk">
--->
<cfif isDefined("form.Doit") and len(trim(form.Notice))>
<cffile action="write" file="#Fullname#" output="#form.Notice#" addnewline="Yes">
</cfif>
<cfif fileExists("#Fullname#")>
<cffile action="read" file="#Fullname#" variable="NoteText">
<cfelse>
[There is no message text file yet.]
</cfif>
<cfparam name="NoteText" default=" ">
<form name="Setnotice" method="post" action="noticefi.cfm">
<textarea name="Notice" wrap="physical" rows="5" cols="60">
<cfoutput>#trim(NoteText)#</cfoutput></textarea>
<input type="Submit" name="Doit" value="Change">
</form>
<!--- Uncomment to block remote users
<cfif cgi.remote_addr is not "127.0.0.1">
<cflocation url="http://www.cftipsplus.com">
</cfif>
--->
<cfset Fullname="C:\doc\noteapp.unk">
<!--- <cfset Fullname="#getDirectoryFromPath(cf_template_path)#"&"noteapp.unk">
--->
<cfif isDefined("form.Doit") and len(trim(form.Notice))>
<cffile action="write" file="#Fullname#" output="#form.Notice#" addnewline="Yes">
<!--- Add the next line if the application
hasn't been defined elsewhere --->
<cfapplication name="Notice" sessionmanagement="no">
<cflock name="NoticeWrite" timeout="30" type="exclusive">
<cfset application.notice=form.Notice>
</cflock>
</cfif>
<cfif fileExists("#Fullname#")>
<cffile action="read" file="#Fullname#" variable="NoteText">
<cfelse>
[There is no message text file yet.]
</cfif>
<cfparam name="NoteText" default=" ">
<form name="Setnotice" method="post" action="noticeap.cfm">
<textarea name="Notice" wrap="physical" rows="5" cols="60">
<cfoutput>#trim(NoteText)#</cfoutput></textarea>
<input type="Submit" name="Doit" value="Change">
</form>