4 Replies Latest reply on Mar 14, 2007 5:43 AM by lusciousmango

    CFLOOP within CFSET?

      How can I get the results of a query to be one comma-delimited string? I've been trying to declare a variable using CFSET, as in:

      <CFSET var_name = <CFLOOP query="qname">#field#,</CFLOOP>>

      so that I get: var_name = 'field1,field2,field3' etc?

      (it's either pathetically obvious or really obtuse, and I'm unable to tell the difference at this point)
        • 1. Re: CFLOOP within CFSET?
          Dan Bracuk Level 5
          You can't nest cftags like this:
          <cftag 1<cftag2> >

          You have to loop through the columnlist.

          But the easiest way would be to simply select the list you want. Most dbs have some sort of concatonation function or operator. Of course, they are different from each other.
          • 2. Re: CFLOOP within CFSET
            Level 7
            I'm not sure what you are trying to achieve here?

            For example with the following dataset:

            ColA ColB ColC
            1 aaa bbb ccc
            2 ddd eee fff
            3 ggg hhh iii

            Are you looking for results like:



            If the former you will need to do something like this:
            <cfset aList = "">
            <cfloop list="#aQuery.columnList#" item="col">
            <cfset aList = listAppend(aList,aQuery[col][row]>

            For the latter you could use the valueList() function.
            <cfset aList = valueList(aQuery.ColA)>
            • 3. Re: CFLOOP within CFSET
              Level 7
              > <CFSET var_name = <CFLOOP query="qname">#field#</CFLOOP>

              As others have said: one cannot do that. One cannot *generate* CF
              statements with other CF statements within the same template. If you stop
              and think about how CF works, you'll see it's a nonsensical endeavour.

              However all you need to do is this (sort of thing):

              <cfset var_name = valueList(qName.field)>

              • 4. Re: CFLOOP within CFSET?
                lusciousmango Level 1
                valueList() did it.

                Y'all are the wind beneath my wings.