Ask a Question related to Coldfusion Database Access, Design and Development.

  1. #1

    Default Query+Array

    Is there any way to store query resultS into an array so we could loop through it one by one.
    incubusm Guest

  2. Similar Questions and Discussions

    1. I want to return an array instead of a query
      Dear Forum, My Flash program uses a web service connector that connects to a CFC. Everything works well with "return type="query. Now, I would...
    2. Sort array from a Query
      I have 2 tables (simplified): coasters (COASTER_CODE, BREWERY_CODE, etc) breweries (BREWERY_CODE, BREWERY) For each brewery I want to count...
    3. Query Stored In array
      Hello, How can store queried data in to two dimenensional array. plz syntax. Thanks in advance Mike
    4. Feed query an Array
      I grab some stuff out of a URL and pass it to a system query. I then use the system query to get all the column names for a particular table. I...
    5. [newbie] Array and query
      Hi to all! :) This week i begin to learn php, but now i have a problem... I made a form in which there is the possibility of choosing some...
  3. #2

    Default Re: Query+Array

    You can access query objects like an array. For example

    <!--- not tested --->
    <cfoutput query="yourQuery">
    #yourQuery["someColumn"][currentRow]#
    </cfoutput>
    mxstu Guest

  4. #3

    Default Re: Query+Array

    If you loop through the query, you don't have to worry about arrays.
    Dan Bracuk Guest

  5. #4

    Default Re: Query+Array

    Becareful using <cfoutput query="yourQuery"> I find it to be funky... lol. i do
    use it though.... I like the <cfloop query=queryname> to store variables and
    use them later as you cannot nest cfloop's or cfoutput - well it will let you
    nest cfloops but the answer will be incorrect as it will only give you the 1st
    variable in the top cfloop and not index through them. An example i think you
    are asking about would be as follows.


    <cfquery name="qTasks2" dbtype="query">
    SELECT DISTINCT cd, ea, PROJECT_NICKNAME,pm
    FROM qTasks
    ORDER BY cd,ea
    </cfquery>

    'arrays declared here

    <cfset acd=arraynew(1)>
    <cfset pm_name=arraynew(1)>
    <cfset aea=arraynew(1)>

    <cfif #qTasks.RecordCount# is 0>
    <h4>No EA with activities planned within request range</h4>
    <cfabort>
    </cfif>
    <cfset i = 0>
    <cfloop query="qTasks2">
    <cfset i = i+1 >
    'arrays defined here
    <cfoutput>
    <cfset acd=#cd#>
    <cfset aea=#ea#>
    <cfset pm_name=#pm#>
    </cfoutput>
    </cfloop>

    Then later I use it - within another query and yet that query is stored
    inside another set of arrays .. 2 diminsion in this case
    ..

    <cfloop index="i" from="1" to ="#qTasks2.recordcount#">
    <cfquery name="qWBS" dbtype="query">
    SELECT distinct cd, ea, startdate, enddate, wbslevel
    FROM qTasks
    WHERE cd='#acd#' AND ea='#aea#'
    </cfquery>
    <cfset j = 0>
    <!--- <cfdump var="#qWBS#"> --->
    <cfoutput query="qWBS">
    <cfset j = j+1 >
    <cfset awbslevel[j]=#wbslevel#>
    <cfset astartdate[j]=#startdate#>
    <cfset aenddate[j]=#enddate#>
    </cfoutput>
    <cfset taskwbscount=j>
    </cfloop>


    bottom line is if your going to do anymore very simple checks then you can use
    cfloop... with the query variables
    from it, but any deeper like nesting then define the arrays and use them....
    with that said, if you can accomplish this
    with database queries then let it do it as it's more powerful than using cfml
    code... server memory etc etc..

    rob

    RobBurn Guest

  6. #5

    Default Re: Query+Array

    >well it will let you nest cfloops but the answer will be incorrect as it will
    only give you
    >the 1st variable in the top cfloop and not index through them

    As you said, you can use nested loops, you just need to use a from/to loop
    instead of < cfloop query="..." >. However, since you can already access query
    values using array syntax:

    yourQuery[colName][rowNumber]

    ... a separate array is often unnecessary and can even be a waste of memory,
    if the goal can be accomplished using an existing query.

    You might want to respost your example using the "attach code" option. The
    code displayed in the html of your post may not be exactly as you posted it.
    The forums interpret [ i ] (with no spaces in between the brackets) as the
    symbol for italic text :-)

    On a side note, there are some unecessary pound "#" signs in the code. As per
    recommendations, pound signs should not be used here:

    <cfif #qTasks.RecordCount# is 0>
    <cfset awbslevel[j]=#wbslevel#>


    ... instead use ...


    <cfif qTasks.RecordCount is 0>
    <cfset awbslevel[j]= wbslevel >




    mxstu Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139