Skip to main content
Ben Nadel at CF Summit West 2024 (Las Vegas) with: Sam Effa
Ben Nadel at CF Summit West 2024 (Las Vegas) with: Sam Effa

Neat Trick With ValueList()

By
Published in Comments (7)

This is just a quick little trick for outputting a list of items with proper spacing. You can use ColdFusion ValueList() using a delimiter that has both a comma AND a space in it: ", ". While this is not really a valid delimiter, which is defined as one character, the ValueList() method will still use both characters when creating the delimited list. Now, you have a nicely spaced list.

<cfquery name="qTags" datasource="...">
	SELECT
		name
	FROM
		tag
	ORDER BY
		name ASC
</cfquery>

<!--- Output nicely formatted tag list. --->
Tags: #ValueList( qTags.name, ", " )#

Want to use code from this post? Check out the license.

Reader Comments

15 Comments

I love CF's list functions. :)

Of course, for the other sort of lists, you can also do this:
<ul><li>#ValueList( qTags.name, "</li><li>" )#</li></ul>

which has come in handy for me on a couple of occasions.

3 Comments

I'm doing a zip code radius search which I do in another query before pulling my list of companies from my company table. The list of zip codes goes into my search query and are listed in order of distance from the source zip code. So I want the output of the second to be the same order as the value list...

Any ideas?

15,902 Comments

@Branden,

I'm not quite sure I follow what you mean; but as @Laurence pointed out, a query-of-queries can help you address values on a one-off basis.

1 Comments

What would happen if you wanted to properly html-escape these values in order to prevent cross-site-scripting attacks, or even just display bugs?
(e.g. what if we're talking about algebra, and one of my tags is "a < b > c"? Do you just turn the rest of your website bold?)

57 Comments

Jerry, if you're using Ben's example, you just run HtmlEditFormat (or similar) on the value produced by ValueList.

For my <li>-based example, it would probably be simplest to loop through manually and call HtmlEditFormat on each one (and if you're doing that, you might as well manually build the string instead of using ValueList).

Of course, it would be handy if the ValueList function had the ability to pass a callback function to allow you to process each item before appending it to the list, but unfortunately that's not possible (well, unless you were to write your own ValueList function to do it).

2 Comments

Thanks! Exactly what I was looking for to do a comparison of two data sets and highlight duplicate results contained in the ValueList using a cfif.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel