3

Hello I have query array with values

  • cat_1 abc
  • cat_1 bbb
  • cat_1 ccc
  • cat_2 abc
  • cat_2 ooo
  • cat_3 ppo

now I have to display this array in the following manner

cat_1

  • abc
  • bbb
  • ccc

cat_2

  • abc
  • ooo

cat_3

  • ppo

I have using a table with associations to get join data from categories and item tables but when I output it to my view. I am not sure how to get the next value of the row in the query loop.

<cfloop query="checklist">
#checklist.categoriesname# #checklist.name#
</cfloop>

Above is my current loop, I want to get the categoriesname once and if is the same as last not output it.

Leigh
  • 28,765
  • 10
  • 55
  • 103
Saad A
  • 1,135
  • 2
  • 21
  • 46
  • 4
    (Edit) Have you looked at using a grouped cfoutput instead of a cfloop? ie ``. It does exactly what you need. See [this example](http://stackoverflow.com/questions/5170553/getting-all-sub-records-in-a-one-to-many-relationship/5170598#5170598). However, to answer the question asked, you can access any query row using array notation, ie `queryName.columnName[RowNumber]`. Though in this case a grouped output is simpler. – Leigh Sep 05 '15 at 20:47
  • thank you it helped. http://www.bennadel.com/blog/2359-coldfusion-10---using-the-group-attribute-with-cfloop-to-group-query-rows.htm this link is also useful – Saad A Sep 05 '15 at 21:03
  • sorry I accidently flag your comments, dunno how to undo – Saad A Sep 05 '15 at 21:04
  • Heh, no worries. Not sure you can undo a "flag". If not, a moderator will probably read the comments and realize it was done accidentally and dismiss it. – Leigh Sep 05 '15 at 21:32
  • Oh yeah, I totally forgot cfloop had a group attribute now too. I was going to vote to close as a duplicate, but you should post the cfloop tip as an answer :) – Leigh Sep 05 '15 at 21:33

1 Answers1

3

As suggested above, code resolve the issue for me.

<cfloop query="checklist" group="categoriesname">
#checklist.categoriesname#
<cfloop>
#checklist.name#
</cfloop>
</cfloop>
Saad A
  • 1,135
  • 2
  • 21
  • 46
  • Within #checklist.categoriesname# and #checklist.name# need a tag, to actually display the values. – Alex Baban Sep 06 '15 at 18:03
  • One important note for those unfamiliar with [the "group" feature](https://wikidocs.adobe.com/wiki/display/coldfusionen/cfloop%3A+looping+over+a+query), in order for it to work properly, the underlying query results *must* be sorted by the grouped column. From the docs, the group feature: *"...Eliminates adjacent duplicate rows when data is sorted. Use if you retrieved a recordset ordered on one or more query columns. For example, if a recordset is ordered on "Customer_ID", you can group the output on "Customer_ID.".* – Leigh Sep 06 '15 at 18:55