3 Replies Latest reply on Oct 28, 2015 11:47 AM by EddieLotter

    How to output cfquery to array?

    pirlo89

      Hello everyone,

      I'm trying to output my cfquery to array. I need something like this:

       

      [code]

      <cfset myarray=arrayNew(2)>

      <cfloop query="qryCustomers">

           <cfset myarray = [{"CustomerID":ID,"Name":FirstLast}]>

      </cfloop>

      [/code]

       

      I tried this code but that did not work. Error always indicate that something is wrong in my cfset where I have my ID and Customer name together. I'm not sure either if I can do this in cold fusion. I have to put these to values together because I have to use them later to compare with other values from second query. If anyone can tell me what is wrong in my code I would appreciate that. Thanks in advance.

        • 1. Re: How to output cfquery to array?
          EddieLotter Level 3
          One way to do it:
          <cfloop query="qryCustomers">
               <cfset myarray[#currentRow#][1] = #ID#>
               <cfset myarray[#currentRow#][2] = #FirstLast#>
          </cfloop>
          Cheers
          Eddie
          • 2. Re: How to output cfquery to array?
            pirlo89 Level 1

            How I can create a structure first and then add it to an array?

             

            So I tried something like this but still does not work.

             

            [CODE]

            <cfset recs = StructNew()>

               <cfloop query="qryTest">

                    <cfset recs[currentRow] = StructNew()>

                    <cfset recs[currentRow].Customer = ' ' & qryTest.CustomerID>

                     <cfset recs[currentRow].Number = ' ' & qryTest.Number>

              </cfloop>

                

              <cfset records = [{CustomerID = "#CustomerID#",Number = "#Number#"}] />

              <cfset fncResults.data = recs>

            [/CODE]

            • 3. Re: How to output cfquery to array?
              EddieLotter Level 3

              That's a different question, but here is how you do it:

               

              <cfset myarray = arrayNew(1)>

              <cfloop query="qryTest">

              <cfset recs = structNew()>

              <cfset recs.Customer = CustomerID>

              <cfset recs.Number = Number>

              <cfset arrayAppend(myarray, recs)>

              </cfloop>

              <cfdump var="#myarray#">

              (Sorry about the poor formatting.)

               

              Cheers

              Eddie