CF Loop and CF File

writing a .txt file with cffile via cfloop

Page 1 of 1

8 Replies - 5825 Views - Last Post: 22 April 2009 - 06:45 AM Rate Topic: -----

#1 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

CF Loop and CF File

Post icon  Posted 17 March 2009 - 11:02 AM

Hello and thanks for your time. I am attempting to write a text (.txt) file with data from a database query. It works fine, but it only writes the first record it finds. I would like to implement a loop so it cycles through each record and writes them all out. Here's what I have so far...

<cfquery name="feed" datasource="rss">
SELECT caption,link,active
FROM feeds
WHERE active = 1
</cfquery>


	<cfoutput>	  
	  
		  <cffile action="write"
			   file="#currentPath#scroller/scrollerData.txt"
				   output="Caption is:#feed.caption#">
								
				</cfoutput>



As you can see the code executes and creates scrollerData.txt with "Caption is:"the caption in the database", but it only creates the first record it finds. I have tried several implementations of cfloop to try to get it to create all the captions that it finds, but to no avail. Any tips will be greatly appreciated, thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: CF Loop and CF File

#2 xheartonfire43x  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: CF Loop and CF File

Posted 17 March 2009 - 12:01 PM

I am not sure if this will actually work because I don't know if it is possible to write it to a file but put the query into an array

<cfquery name="feed" datasource="rss">
SELECT caption,link,active
FROM feeds
WHERE active = 1
</cfquery>

  <cfset captionArray = ArrayNew(1)>
  <cfset count = 0>
  <cfloop query="feed">
	<cfset count = #count# + 1>
	<cfset captionArray[#count#] = #feed.caption#>
  </cfloop>

		  <cffile action="write"
			   file="#currentPath#scroller/scrollerData.txt"
				   output="Caption is:#captionarray#">
							   



If it doesn't work let me know.
Was This Post Helpful? 0
  • +
  • -

#3 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

Re: CF Loop and CF File

Posted 17 March 2009 - 12:21 PM

Yes! I just discovered the array in my research when I noticed your reply. This must mean I am on the right track. I am implementing an array strategy right now to see what I come up with. Will post findings asap. Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

Re: CF Loop and CF File

Posted 18 March 2009 - 05:56 AM

Hello Again! Based on the array suggestion I have come up with the following which works great except it is only writing a single record from the query to the .txt file.

<cfquery name="feed" datasource="rss">
SELECT *
FROM feeds
WHERE active = 1
</cfquery>

<cfset total_records=feed.recordcount>

<cfdump var=#total_records#>

<!--- Now, create a loop to output the array contents --->
<!--- Declare the array --->
<cfset myarray=arraynew(2)>

<!--- Populate the array row by row --->
<cfloop query="feed">
<cfset myarray[CurrentRow][1]=link>
<cfset myarray[CurrentRow][2]=caption>
</cfloop>

<cfdump var=#myarray#>

<!--- Now, create a loop to output the array contents --->
<cfloop index="Counter" from=1 to="#total_records#">
<cfoutput>
<cffile
action = "write"
file="#currentPath#scroller/scrollerData.txt"
output='{"data":"<li><div><a href="#MyArray[Counter][1]#">#MyArray[Counter][2]#</a></div></li>"},'
addnewline="yes">
</cfoutput>
</cfloop>



The <cfdump> of #total_records# reveals that I have 15 records in the database to pull from, and the <cfdump> of #myarray#, after it is populated, shows all 15 records from the database.
So, I believe I am running into trouble at the loop to output the array contents.

Thanks for any insight you may be able to provide!
Was This Post Helpful? 0
  • +
  • -

#5 xheartonfire43x  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: CF Loop and CF File

Posted 18 March 2009 - 06:25 AM

View Postmidasxl, on 18 Mar, 2009 - 04:56 AM, said:

Hello Again! Based on the array suggestion I have come up with the following which works great except it is only writing a single record from the query to the .txt file.

<cfquery name="feed" datasource="rss">
SELECT *
FROM feeds
WHERE active = 1
</cfquery>

<cfset total_records=feed.recordcount>

<cfdump var=#total_records#>

<!--- Now, create a loop to output the array contents --->
<!--- Declare the array --->
<cfset myarray=arraynew(2)>

<!--- Populate the array row by row --->
<cfloop query="feed">
<cfset myarray[CurrentRow][1]=link>
<cfset myarray[CurrentRow][2]=caption>
</cfloop>

<cfdump var=#myarray#>

<!--- Now, create a loop to output the array contents --->
<cfloop index="Counter" from=1 to="#total_records#">
<cfoutput>
<cffile
action = "write"
file="#currentPath#scroller/scrollerData.txt"
output='{"data":"<li><div><a href="#MyArray[Counter][1]#">#MyArray[Counter][2]#</a></div></li>"},'
addnewline="yes">
</cfoutput>
</cfloop>



The <cfdump> of #total_records# reveals that I have 15 records in the database to pull from, and the <cfdump> of #myarray#, after it is populated, shows all 15 records from the database.
So, I believe I am running into trouble at the loop to output the array contents.

Thanks for any insight you may be able to provide!


Thats because you are creating 15 files that keep overwriting eachother. You need to loop everything into the array and write the array into the file.

<cfquery name="feed" datasource="rss">
SELECT *
FROM feeds
WHERE active = 1
</cfquery>

<cfset total_records=feed.recordcount>

<cfdump var=#total_records#>

<!--- Now, create a loop to output the array contents --->
<!--- Declare the array --->
<cfset myarray=arraynew(2)>

<!--- Populate the array row by row --->
<cfset currentrow = 0>
<cfloop query="feed">
<cfset currentrow = currentrow + 1>
<cfset myarray[CurrentRow][#currentrow#,1]=link>
<cfset myarray[CurrentRow][#currentrow#,2]=caption>
</cfloop>

<cfdump var=#myarray#>

<!--- Now, create a loop to output the array contents --->
<cffile
action = "write"
file="#currentPath#scroller/scrollerData.txt"
output="#myArray#">
</cfoutput>
</cfloop>




An array acts just like a database table. The array that I did will put the link into the first column of it and the caption into the second column of it. It will then loop through the query creating as many rows as the table has and then outputs it to the cffile. It should work just like that.
Was This Post Helpful? 0
  • +
  • -

#6 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

Re: CF Loop and CF File

Posted 18 March 2009 - 07:25 AM

Ok, I just accidentally figured it out. In my full on trial and error assault I stumbled across the following which works perfectly for my desired result...

<cfquery name="feed" datasource="rss">
SELECT *
FROM feeds
WHERE active = 1
</cfquery>

<cfloop query="feed">

<cfset data='{"data":"<li><div><a href="#feed.link#">#feed.caption#</a></div></li>"},'>

<cffile action="append"
		file="#currentPath#scroller/scrollerData.txt"
		output="#data#">

</cfloop>



So simple. Instead of cffile action="write" I tried "append" and BAM! Perfect!

Thanks for your help!

This post has been edited by midasxl: 18 March 2009 - 08:55 AM

Was This Post Helpful? 0
  • +
  • -

#7 xheartonfire43x  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 454
  • Joined: 22-December 08

Re: CF Loop and CF File

Posted 18 March 2009 - 12:38 PM

View Postmidasxl, on 18 Mar, 2009 - 06:25 AM, said:

Ok, I just accidentally figured it out. In my full on trial and error assault I stumbled across the following which works perfectly for my desired result...


So simple. Instead of cffile action="write" I tried "append" and BAM! Perfect!

Thanks for your help!


Did what I said not work at all? Or was it just not what you were loking for?
Was This Post Helpful? 0
  • +
  • -

#8 smith2009  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 20-March 09

Re: CF Loop and CF File

Posted 28 March 2009 - 02:26 AM

Hi,

Here is the solution for you.
<cfoutput query="feed">	  
	 
		  <cffile action="write"
			   file="#currentPath#scroller/scrollerData.txt"
				   output="Caption is:#caption#">
							   
				</cfoutput>



If you dont' specify the query in the cfoutput ColdFusion will only look at the first record.
I hope it will help you.

Cheers,

:)

_____________________________
Smith
Beginner in ColdFusion Development.
Was This Post Helpful? 0
  • +
  • -

#9 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 193
  • Joined: 03-December 08

Re: CF Loop and CF File

Posted 22 April 2009 - 06:45 AM

View Postxheartonfire43x, on 18 Mar, 2009 - 11:38 AM, said:

View Postmidasxl, on 18 Mar, 2009 - 06:25 AM, said:

Ok, I just accidentally figured it out. In my full on trial and error assault I stumbled across the following which works perfectly for my desired result...


So simple. Instead of cffile action="write" I tried "append" and BAM! Perfect!

Thanks for your help!


Did what I said not work at all? Or was it just not what you were loking for?


xheartonfire43x,
Very sorry, I did not notice your response. Your solution was fine, but as you speculated it was not exactly what I needed for the scope of that project. Sorry! Thanks for your help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1