#field1#
#f" /> #field1#
#f" /> #field1#
#f"/>

output database data using cfscript

142 Views Asked by At

I'm not familiar with cfscript having used tags for ever so not sure how to output a database query.

Normally

<cfoutput query="qName">
#field1#<br>
#field2#<br><br>
</cfoutput>

How do I do that in cfscript?


Updated as per the comment below and using tags but trying now to find a way to split each query line onto a new line

<cfset dataSet = "">
<cfoutput query="Bookings">

<cfset dataSet = dataSet & ',' & '#str_BookingRef#'>
<cfset dataSet = dataSet & ',' & '#str_GuestName#'>
<cfset dataSet = dataSet & ',' & '#str_CottageName#'>
<cfset dataSet = dataSet & ',' & '#str_AgentName#'>
<cfset dataSet = dataSet & ',' & '#dtm_StartDate#'>
<cfset dataSet = dataSet & ',' & '#dtm_EndDate#'>
<cfset dataSet = dataSet & ',' & '#int_Income#'>
<cfset dataSet = dataSet & ',' & '#int_AgentFee#'>
<cfset dataSet = dataSet & ',' & '#int_VatAmount#'>
<cfset dataSet = dataSet & ',' & '#int_ToOwner#'>
<cfset dataSet = dataSet & ',' & '#int_Adults#'>
<cfset dataSet = dataSet & ',' & '#int_Children#'>
<cfset dataSet = dataSet & ',' & '#int_Pets#' & 'evaluate(Chr(13))'>

</cfoutput>

<cfdump var="#dataSet#">

<cfscript>
data = QueryNew("str_BookingRef, str_GuestName, str_CottageName, str_AgentName, dtm_StartDate, dtm_EndDate, int_Income, int_AgentFee, int_VatAmount, int_ToOwner, int_Adults, int_Children, int_Pets", "VarChar,VarChar,VarChar,VarChar,VarChar,VarChar,VarChar,VarChar,VarChar,VarChar,VarChar,VarChar,VarChar", [#dataSet#]);
spreadsheet = New spreadsheetLibrary.Spreadsheet();
workbook = spreadsheet.workbookFromQuery( data );
path = "C:\Inetpub\vhosts\pathtofile\#session.int_OwnerID#.xls";
spreadsheet.write( workbook, path, true );
</cfscript>
2

There are 2 best solutions below

0
Dan Bracuk On

Something like this will accomplish your goal.

for (RowNumber = 1; RowNumber <= qName.RecordCount; RowNumber++) {
    writeoutput(qName[field1][RowNumber] & '<br>')
    etc
}

But personally, I'd stick to the tag.

0
James A Mohler On

You don't want to build a string and hope it is JSON as an intermediate step.

What you want is

spreadsheet = spreadsheetNew();
spreadsheet.addRows( Bookings );

It is possible that Bookings does not have the columns in the right order. In that case, use query of queries to make a query with stuff in the right order

OR

If you really wanted to build the results row by row.

   spreadsheet = spreadsheetNew();
   for (row in Bookings) {
      spreadsheet.addRow("#str_BookingRef#,
        #str_GuestName#,
        #str_CottageName#,
        #str_AgentName#, 
        #dtm_StartDate#,
        #dtm_EndDate#,
        #int_Income#,
        #int_AgentFee#,
        #int_VatAmount#,
        #int_ToOwner#,
        #int_Adults#,
        #int_Children#,
        #int_Pets#");
   }

Note: The data is separated to new lines, but if you were to do it this way, it would have to be all on the same line.