freelanceprogrammers.org Forum Index » Cold Fusion

more elegant code for comma-separated list


View user's profile Post To page top
rgpg_99 Posted: Mon Sep 12, 2005 5:41 am


Joined: 01 Apr 2005

Posts: 16
more elegant code for comma-separated list
Hi all,

I`m capturing a number of items that I would like to store as separate
items in a table (no surprise here), but also as one (composite) field as a
comma-separated list. To achieve the second I have this code before the
INSERT query:

<cfset compositeList = "">
<cfif #FORM.item1# NEQ ""><cfset compositeList = FORM.item1></cfif>
<cfif #FORM.item2# NEQ ""><cfset compositeList = compositeList & ", " &
FORM.item2></cfif>
<cfif #FORM.item3# NEQ ""><cfset compositeList = compositeList & ", " &
FORM.item3></cfif>
<cfif #FORM.item4# NEQ ""><cfset compositeList = compositeList & ", " &
FORM.item4></cfif>
<cfif #FORM.item5# NEQ ""><cfset compositeList = compositeList & ", " &
FORM.item5></cfif>
<cfif #FORM.item6# NEQ ""><cfset compositeList = compositeList & ", " &
FORM.item6></cfif>


...and then I just insert the resulting "compositeList" variable into one
single field as a comma-separated list. Could anyone suggest a more elegant
way of coding this/achieving the same result?

Thanks in advance,

Roberto Perez
rgpg@...
Reply with quote
Send private message
View user's profile Post To page top
wilsose Posted: Mon Sep 12, 2005 5:45 pm


Joined: 12 Sep 2005

Posts: 1
more elegant code for comma-separated list
Here is some code thats a little more "elegant". At least if you
increase the number of items in your form all you have to do is
increase the to parameter in the <cfloop>.


<cfset compositeList="">

<cfloop index="x" from="1" to="6" step="1">
<cfif IsDefined("Form.item" & x) AND Evaluate("Form.item" & x) NEQ ``>
<cfset compositeList=ListAppend(compositeList, Evaluate("Form.item" &
x))>
</cfif>
</cfloop>

Hope this helps,

Sean Wilson
plusLogic

--- In cold_fusion@yahoogroups.com, Roberto Perez <rgpg@t...> wrote:
> Hi all,
>
> I`m capturing a number of items that I would like to store as
separate
> items in a table (no surprise here), but also as one (composite)
field as a
> comma-separated list. To achieve the second I have this code before
the
> INSERT query:
>
> <cfset compositeList = "">
> <cfif #FORM.item1# NEQ ""><cfset compositeList = FORM.item1></cfif>
> <cfif #FORM.item2# NEQ ""><cfset compositeList = compositeList & ",
" &
> FORM.item2></cfif>
> <cfif #FORM.item3# NEQ ""><cfset compositeList = compositeList & ",
" &
> FORM.item3></cfif>
> <cfif #FORM.item4# NEQ ""><cfset compositeList = compositeList & ",
" &
> FORM.item4></cfif>
> <cfif #FORM.item5# NEQ ""><cfset compositeList = compositeList & ",
" &
> FORM.item5></cfif>
> <cfif #FORM.item6# NEQ ""><cfset compositeList = compositeList & ",
" &
> FORM.item6></cfif>
>
>
> ...and then I just insert the resulting "compositeList" variable
into one
> single field as a comma-separated list. Could anyone suggest a more
elegant
> way of coding this/achieving the same result?
>
> Thanks in advance,
>
> Roberto Perez
> rgpg@t...
Reply with quote
Send private message
Post new topic Reply to topic
Display posts from previous:   
 

All times are GMT
Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Freelace Website Designer - Customer web design and software building.
China Wholesale - Electronics Products
Character Studio - Tutorials and Help