Gosi
Gosi

Reputation: 2003

ColdFusion ListAppend

I am having difficulties trying to use ListAppend.

I have data from a table and this is my code.

<cfquery name="getData" datasource="test">
  select * from test;
</cfquery>

Now what I want to do is to make all the values in the column name which I have named it as nm_column into a list using ListAppend.

<cfset dataList = ListAppend('', '#getData.nm_column#')>
<cfoutput>#dataList#</cfoutput>

What this does is that it only shows the 1st value of the nm_colum. I do understand that I am missing the loop part, thats why its only showing me just the 1st value. So how do I loop it and get all the values to it?

I tried this, but it did not work.

<cfset dataList = ListAppend('', '<cfloop query="getData">#getData.nm_column#</cfloop>')>

So can someone please teach me the way to properly write that code?

Upvotes: 0

Views: 2969

Answers (1)

Matt Busche
Matt Busche

Reputation: 14333

There's a built in function in ColdFusion that will do this for you.

<cfset dataList = valueList(getData.nm_column)>

As far as the issue with your code, listAppend's first argument is the list you're adding things to. Also, you cannot nest ColdFusion tags like that. The code will not compile.

If you want to loop through something to append to a list, this is what you would do.

<cfset dataList = ''>
<cfloop query="getData">
  <cfset dataList = listAppend(dataList, nm_column)>
</cfloop>

This would be terrible for performance though since a string is immutable. If you really needed to add items to a list through a lip I would create an array and then use arrayToList to convert that array to a list.

Upvotes: 6

Related Questions