1
\$\begingroup\$

I'm outputting the results of a query to file.txt in JSON. The file is updated every time I click on 'submit'(form) and depending on the variables it updates the query.

Right now the table only has about 20 records and will probably increase by 2/3 new records everyday.

Is there a better way to updating the file test.txt getting the results from a query?

index.cfm:

  <cfsavecontent variable="mytest"> 
    [
    <cfoutput query="getimeline"> 
    <cfif (getimeline.CurrentRow EQ getimeline.RecordCount)>
    {"title":"#name_time#","date":"#dateformat(date_time,"mmmm d, yyyy")#"}
    <cfelse>
    {"title":"#name_time#","date":"#dateformat(date_time,"mmmm d, yyyy")#"},
    </cfif>

    </cfoutput> 
    ]
    </cfsavecontent> 

    <cffile action="write" file="C:test.txt"  output="#mytest#"> 

test.txt:

[

{"title":"Advance pay Rollovers","date":"October 23, 2015"},

{"title":"New Card Ordering Process 1","date":"December 28, 2015"},

{"title":"about about","date":"February 5, 2016"}

]
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

I don't know sqat about , but it seems you could eliminate some duplication:

<cfsavecontent variable="mytest"> 
    [
    <cfoutput query="getimeline"> 
    {"title":"#name_time#","date":"#dateformat(date_time,"mmmm d, yyyy")#"}<cfif (getimeline.CurrentRow NEQ getimeline.RecordCount)>,</cfif>    
    </cfoutput> 
    ]
</cfsavecontent> 

<cffile action="write" file="C:test.txt"  output="#mytest#"> 

If you don't mind that the comma will get placed on the next line, then you can use this more readable writing style:

<cfsavecontent variable="mytest"> 
    [
    <cfoutput query="getimeline"> 
    {"title":"#name_time#","date":"#dateformat(date_time,"mmmm d, yyyy")#"}
    <cfif (getimeline.CurrentRow NEQ getimeline.RecordCount)>,</cfif>    
    </cfoutput> 
    ]
</cfsavecontent> 

<cffile action="write" file="C:test.txt"  output="#mytest#"> 
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.