?
Solved

How do I close up a blank line in cffile output?

Posted on 2008-11-19
5
Medium Priority
?
504 Views
Last Modified: 2012-08-13
In the attached code snippet I am creating a .csv file for downloading to Word.

On some records the "Address2" field and "Fax" fields are blank. When these fields are blank the output contains a blank line.  I want to close up all blank lines from the output.

Thanks in advance for any help.
<cfoutput>
  <cfloop query="Recordset1">
    <cffile action="append"
    addnewline="yes"
    file="#tempfile#"
    output="
#TRIM(RecordSet1.company_name)#
#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)# #IIF("Recordset1.certifications" IS NOT "",TRIM("RecordSet1.certifications"),"")#
#TRIM(RecordSet1.address1)# 
#IIF("RecordSet1.address2" neq "",TRIM("RecordSet1.address2"),DE(""))#
#TRIM(RecordSet1.city)#, #TRIM(RecordSet1.st)# #TRIM(RecordSet1.zip)#
Phone: #TRIM(RecordSet1.phone)# #IIF("RecordSet1.ext" IS NOT "",TRIM("RecordSet1.ext"),DE(""))#
#IIF(TRIM(RecordSet1.fax) neq "",DE("Fax: "),DE(""))##IIF(TRIM(RecordSet1.fax) neq "",TRIM("RecordSet1.fax"),DE(""))#
#IIF("RecordSet1.email" neq "",TRIM("RecordSet1.email"),DE(""))#
#IIF("RecordSet1.website" neq "",TRIM("RecordSet1.website"),DE(""))#
#IIF("RecordSet1.description" neq "",TRIM("RecordSet1.description"),DE(""))#"
>
     
</cfloop>
</cfoutput>

Open in new window

0
Comment
Question by:edfm
  • 3
5 Comments
 
LVL 7

Accepted Solution

by:
black0ps earned 1500 total points
ID: 23000224
Maybe try saving the content before you put it in the cffile. Put it in a variable, then output the variable. This way you can use <cfif> instead of all of the iif().
<cfoutput>
<cfsavedcontent variable="myVar">
#TRIM(RecordSet1.company_name)#
#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)# #IIF("Recordset1.certifications" IS NOT "",TRIM("RecordSet1.certifications"),"")#
#TRIM(RecordSet1.address1)# 
#IIF("RecordSet1.address2" neq "",TRIM("RecordSet1.address2"),DE(""))#
#TRIM(RecordSet1.city)#, #TRIM(RecordSet1.st)# #TRIM(RecordSet1.zip)#
Phone: #TRIM(RecordSet1.phone)# #IIF("RecordSet1.ext" IS NOT "",TRIM("RecordSet1.ext"),DE(""))#
#IIF(TRIM(RecordSet1.fax) neq "",DE("Fax: "),DE(""))##IIF(TRIM(RecordSet1.fax) neq "",TRIM("RecordSet1.fax"),DE(""))#
#IIF("RecordSet1.email" neq "",TRIM("RecordSet1.email"),DE(""))#
#IIF("RecordSet1.website" neq "",TRIM("RecordSet1.website"),DE(""))#
#IIF("RecordSet1.description" neq "",TRIM("RecordSet1.description"),DE(""))#
</cfsavedcontent>
  <cfloop query="Recordset1">
    <cffile action="append"
    addnewline="yes"
    file="#tempfile#"
    output="#myVar#">
     
</cfloop>
</cfoutput>

Open in new window

0
 

Author Comment

by:edfm
ID: 23001291
Thanks to blackOps for putting for the suggestion.

The solution offered by blackOps put me on the right track to a solution. I have attached the code that worked for me.  Instead of trying to close up the blank lines created by Carriage returns created by an "Enter"  after each link of code, I put all the code on one line and used cfif statement to insert a Chr(10) line feed at the desired locations.  
<cffile action="write"
	addnewline="yes" 
	file="#tempfile#" 
    output=" ">
 
 
  <cfloop query="Recordset1">
<cfoutput>
<cfsavecontent variable="myVar"><cfif #TRIM(RecordSet1.company_name)# GT "">#TRIM(RecordSet1.company_name)##Chr(10)#</cfif><cfif #TRIM(Recordset1.certifications)# GT "">#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)# #TRIM(Recordset1.certifications)##Chr(10)#<cfelse>#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)##Chr(10)#</cfif><cfif #RecordSet1.address2# CONTAINS "Suite">#TRIM(RecordSet1.address1)#, #TRIM(RecordSet1.address2)##Chr(10)#<cfelse>#TRIM(RecordSet1.address1)##Chr(10)#</cfif><cfif (#TRIM(RecordSet1.address2)# GT "") AND (#TRIM(RecordSet1.address2)# DOES NOT CONTAIN "Suite")>#TRIM(RecordSet1.address2)##Chr(10)#<cfelse></cfif>#TRIM(RecordSet1.city)#, #TRIM(RecordSet1.st)# #TRIM(RecordSet1.zip)##Chr(10)#<cfif #TRIM(RecordSet1.ext)# GT "">Phone: #TRIM(RecordSet1.phone)#  Ext: #TRIM(RecordSet1.ext)##Chr(10)#<cfelseif #TRIM(RecordSet1.phone)# GT "">Phone: #TRIM(RecordSet1.phone)##Chr(10)#</cfif><cfif #TRIM(RecordSet1.fax)# GT "">Fax: #TRIM(RecordSet1.fax)##Chr(10)#<cfelse></cfif><cfif #TRIM(RecordSet1.email)# GT "">#TRIM(RecordSet1.email)##Chr(10)#<cfelse></cfif><cfif #TRIM(RecordSet1.website)# GT "">#TRIM(RecordSet1.website)##Chr(10)#<cfelse></cfif><cfif #TRIM(RecordSet1.description)# GT "">#TRIM(RecordSet1.description)#<cfelse></cfif>
</cfsavecontent>
</cfoutput>
    <cffile action="append"
    addnewline="yes"
    file="#tempfile#"
    output="#myVar#">
     
</cfloop>

Open in new window

0
 

Author Closing Comment

by:edfm
ID: 31518545
Thanks for the help on this. I had to move things around a bit and put everything on one line and then insert #chr(10)# to create the line feeds at the desired places. But the idea of using cfsavecontent allowed me to use cfif statement to make it work. I posted the correct code in a comment on the questions.
0
 

Author Comment

by:edfm
ID: 23001326
Here is the final code that worked for this solution.  

To close up blank lines in cffile output I eliminated all key board generated carriage returns in the cfsavecontent section creating one long line of code. Then used #chr(10)# generated line feeds controlled by cfif statements to get the desired results.

<cffile action="write"
	addnewline="yes" 
	file="#tempfile#" 
    output=" ">
 
 
  <cfloop query="Recordset1">
<cfoutput>
<cfsavecontent variable="myVar"><cfif #TRIM(RecordSet1.company_name)# GT "">#TRIM(RecordSet1.company_name)##Chr(10)#</cfif><cfif #TRIM(Recordset1.certifications)# GT "">#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)# #TRIM(Recordset1.certifications)##Chr(10)#<cfelse>#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)##Chr(10)#</cfif><cfif #RecordSet1.address2# CONTAINS "Suite">#TRIM(RecordSet1.address1)#, #TRIM(RecordSet1.address2)##Chr(10)#<cfelse>#TRIM(RecordSet1.address1)##Chr(10)#</cfif><cfif (#TRIM(RecordSet1.address2)# GT "") AND (#TRIM(RecordSet1.address2)# DOES NOT CONTAIN "Suite")>#TRIM(RecordSet1.address2)##Chr(10)#<cfelse></cfif>#TRIM(RecordSet1.city)#, #TRIM(RecordSet1.st)# #TRIM(RecordSet1.zip)##Chr(10)#<cfif #TRIM(RecordSet1.ext)# GT "">Phone: #TRIM(RecordSet1.phone)#  Ext: #TRIM(RecordSet1.ext)##Chr(10)#<cfelseif #TRIM(RecordSet1.phone)# GT "">Phone: #TRIM(RecordSet1.phone)##Chr(10)#</cfif><cfif #TRIM(RecordSet1.fax)# GT "">Fax: #TRIM(RecordSet1.fax)##Chr(10)#<cfelse></cfif><cfif #TRIM(RecordSet1.email)# GT "">#TRIM(RecordSet1.email)##Chr(10)#<cfelse></cfif><cfif #TRIM(RecordSet1.website)# GT "">#TRIM(RecordSet1.website)##Chr(10)#<cfelse></cfif><cfif #TRIM(RecordSet1.description)# GT "">#TRIM(RecordSet1.description)#<cfelse></cfif>
</cfsavecontent>
</cfoutput>
    <cffile action="append"
    addnewline="yes"
    file="#tempfile#"
    output="#myVar#">
     
</cfloop>

Open in new window

0
 
LVL 16

Expert Comment

by:duncancumming
ID: 23001838
Can I make some suggestions?  

Firstly, you don't need to wrap variables in # # inside most cf tags, such as CFIF.  e.g.
<cfif #TRIM(RecordSet1.company_name)# GT "">
can be written as
<cfif TRIM(RecordSet1.company_name) GT "">
This is considered best practice, takes less typing, should run fractionally quicker, and is easier to read.

Secondly, I'm not sure about the method you're using for checking string length
<cfif TRIM(RecordSet1.company_name) GT "">
While technically I suppose that would work, I think what you actually mean to do is
<cfif Len(TRIM(RecordSet1.company_name)) GT 0>
or perhaps just
<cfif TRIM(RecordSet1.company_name) NEQ "">
i.e. either the string has length > 0, or the string is not an empty string.  To say the string is greater than an empty string is a slightly odd comparison.  

Thirdly another way to do this would be to append each line of your file to a struct, list or array, then output that to your file.  I'd recommend the array.
<cfset fileArray = ArrayNew(1)>
<cfif Len(TRIM(RecordSet1.company_name))>
	<cfset ArrayAppend(fileArray, "#TRIM(RecordSet1.company_name)#")>
</cfif>
<cfif Len(TRIM(Recordset1.certifications))>
	<cfset ArrayAppend(fileArray, "#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)# #TRIM(Recordset1.certifications)#")>
<cfelse>
	<cfset ArrayAppend(fileArray, "#TRIM(RecordSet1.fname)# #TRIM(RecordSet1.lname)#")>
</cfif>
etc
 
<cfloop index="i" from="1" to="#ArrayLen(fileArray)#">
	<cffile action="append"
		addnewline="yes"
		file="#tempfile#"
		output="#fileArray[i]#">
</cfloop>

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Change in PHP Behavior with Session Write Short Circuit (http://php.net/manual/en/book.session.php#116217) (Winter 2014)** With the release of PHP 5.6 the session handler changed in a way that many think should be considered a bug.  See the note …
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question