Loop through list, output to csv

Ok, I have this script that loops through a list of ids and it's supposed to output/append each iteration to a csv file but it only outputs data from the first id in the list.  What am I doing wrong?  Here is the script:


<cfset attributes.date = DateFormat(Now(), "yyyymmdd")>

<cfset dailyfile = attributes.date & ".csv"/>
<cfset storageDirectory = "[path to server root]\64\20051229\" />
<cfset filename = storageDirectory & dailyfile />


<cfset choosexid = "71829,73015">

<cfloop index="x" list="#choosexid#">


<cfquery name="dailydump" datasource="aohDB">
select *
from u_user
where id = '#x#'
</cfquery>



<cfset strHeader = '"Field Header"' />
             
<cffile action="write" file="#filename#" output="#strHeader#" addnewline="yes" />
            
<!--- loop through query to spit out the contents --->
<cfset nCount = dailydump.RecordCount />
            
                  <cfloop index="i" from="1" to="#nCount#">
                                          
                  <!--- create a string of the current records data --->
                                          
                  
                  <cfset strCurrentRecord = '"#dailydump.b_lname[i]#"' />
                  
                                          
                  <!--- write the current record to the file --->
                  <cfif i neq nCount>
                  <cffile action="append" file="#filename#" output="#strCurrentRecord#" addnewline="yes" />
                  <cfelse>
                  <cffile action="append" file="#filename#" output="#strCurrentRecord#" />
                  </cfif>
                  
                  </cfloop>
            





</cfloop>
COwebmasterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

btrevarthenCommented:
I modified your code so I didn't need your database and it ran fine (See below)... So the only thing I can think of is that only 1 of the two ID's you are looking for exists in the database.


<cfoutput>
<cfset attributes.date = DateFormat(Now(), "yyyymmdd")>

<cfset dailyfile = attributes.date & ".csv"/>
<cfset storageDirectory = expandPath("\") />
<cfset filename = storageDirectory & dailyfile />

<cfset choosexid = "71829,73015">

<cfloop index="x" list="#choosexid#">
      <cfset strHeader = '"Field Header"' />
      <cffile action="write" file="#filename#" output="#strHeader#" addnewline="yes" />
                    
      <!--- loop through query to spit out the contents --->
      <cfset nCount = 4 />
                    
      <cfloop index="i" from="1" to="#nCount#">
            <!--- create a string of the current records data --->
            <cfset strCurrentRecord = x />
            <!--- write the current record to the file --->
            <cfif i neq nCount>
                  #strCurrentRecord# {newline}
            <cfelse>
                  #strCurrentRecord#
            </cfif>
      </cfloop>
</cfloop>
</cfoutput>
btrevarthenCommented:
Output when I ran that modified code:

                71829 {newline}
            71829 {newline}
            71829 {newline}
            71829 73015 {newline}
            73015 {newline}
            73015 {newline}
            73015
Mr_NilCommented:
Try :

<cfscript>
variables.CRLF = chr(13)&chr(10);

variables.dailyfile = DateFormat(Now(), "yyyymmdd")  & ".csv";
variables.storageDirectory = "[path to server root]\64\20051229\";
variables.filename = storageDirectory & dailyfile;
variables.strHeader = '"Field Header"';

variables.choosexid = "71829,73015";
</cfscript>

<cfquery name="dailydump" datasource="aohDB">
SELECT  b_lname
FROM u_user
WHERE id IN (<cfqueryparam cfsqltype="cf_sql_varchar" list="yes" value="#variables.choosexid#">)
ORDER BY id
</cfquery>

<cfset variables.dumplist = valuelist(dailydump.b_lname,variables.CRLF) />
   
<cffile action="write" file="#variables.filename#" output="#variables.strHeader#" addnewline="yes" />
<cffile action="append" file="#variables.filename#" output="#variables.dumplist#" addnewline="yes" />


What this does is gets all of your records for your 2 choosexid values in one query rather than having to loop through the list.  Your solution above unfortunately overwrites the dump file thats been created each time you loop through the list of choosexid.  The solution I've given you will take all the values in the b_lname column in your query and put them straight into a list seperated by a CRLF.  This list can then be appended to your dumpfile after you've written out the header.

Hopefully this gives you the output you were expecting.

Stephen

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
COwebmasterAuthor Commented:
Ok, let me try that..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.