Skip to main content
Known Participant
July 23, 2008
Answered

Adding database columns in Coldfusion

  • July 23, 2008
  • 3 replies
  • 286 views
Hi everyone...
I have a huge problem at work and I have no idea how to fix it.
-I need to generate a drop-down list.
-This list needs to have one value shown for around 5,000 records from the below data:
-I need to query a database for 3 columns, course_name, course_num, and section_num
-After querying the DB, I need to add the three colums up to something like "EXM-1-1"
-Then I need to make a dropdown list (again, about 5000 items long) with each of these results.
-The types for the 3 columns (respectively) are varchar(7), varchar(20), and mediumint(7)
This topic has been closed for replies.
Correct answer Newsgroup_User
If I read that correctly you just want concatenate the three columns and
display this in a unwieldly long select list? If so here is one way.

<select...>
<cfoutput query="myLongList">
<option
value="...">#myLongList.aVarchar#-#myLongList.bVarchar#-#myLongList.int#</option>
</cfoutput>
</select>

You could also concatenate the three fields into one with your database
concatenation operator. Then you would just have a single field to
output which would facilitate use in a <cfselect...> tag I believe.

3 replies

Inspiring
July 23, 2008
freedomflyer wrote:
> That works...well...great! Thanks so much!
> Just a couple of questions....
>
> 1) I thought I had to externally concatenate the strings by using "&"

You could use the concatenate operator [&] if you would like to
concatenate strings together into one larger string. I.E. <cfset
theLargeStr = aSmallStr & "-" & bSmallStr>.

What I did in the previous code was just output three separate string
variables with two string literals of the '-' character between them.

Interestingly ColdFusion is very forgiving on how one concatenates
strings so that often the two following lines are equivalent. Note the
placement of the pound signs and quotes.

<cfset theLargeStr = aSmallStr & "-" & bSmalStr>
<cfset theLargeStr = "#aSmallStr#-#bSmalStr#">

I think most people would find the first line a bit cleaner and clearer
but otherwise they are the same.

> ...It almost seems like
> #myLongList.aVarchar#-#myLongList.bVarchar#-#myLongList.int#
> would attempt to subtract values. (And yet it works...) So why does this
> happen? Is it not possible to put a value of, say, 5-3 in the option tags and
> have it display "2" as the option?

<cfoutput>#aVar#-#bVar#<cfoutput>
This output two variables separated by a '-' character.

<cfoutput>#aVar-bVar#</cfoutput>
This would output the difference (subtraction) of aVar and bVar.

Note the placement of the pound signs this makes all the difference.

>
> 2) I noticed the <cfoutput/> tags sandwiching my option tags. Does this mean
> that HTML and CF are both very easily intermeshed?

*_TOO_ easily* according to some detractors of ColdFusion.

Yes CFML is designed to be intermingled into the HTML that it is
dynamically building.

This can be easily abused and create horribly hard to understand and
maintain code. So it is always a good practice to not intermingle
unnecessary business logic processing with the display logic. But for a
purpose like this, which was to display a dynamic list of option tags,
this is the way most would write it.
Known Participant
July 23, 2008
That works...well...great! Thanks so much!
Just a couple of questions....

1) I thought I had to externally concatenate the strings by using "&"
...It almost seems like
#myLongList.aVarchar#-#myLongList.bVarchar#-#myLongList.int#
would attempt to subtract values. (And yet it works...) So why does this happen? Is it not possible to put a value of, say, 5-3 in the option tags and have it display "2" as the option?

2) I noticed the <cfoutput/> tags sandwiching my option tags. Does this mean that HTML and CF are both very easily intermeshed?

Thank you again, so much, for your help.
Newsgroup_UserCorrect answer
Inspiring
July 23, 2008
If I read that correctly you just want concatenate the three columns and
display this in a unwieldly long select list? If so here is one way.

<select...>
<cfoutput query="myLongList">
<option
value="...">#myLongList.aVarchar#-#myLongList.bVarchar#-#myLongList.int#</option>
</cfoutput>
</select>

You could also concatenate the three fields into one with your database
concatenation operator. Then you would just have a single field to
output which would facilitate use in a <cfselect...> tag I believe.