Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dealing With Comma's In <SELECT> Values When "Multiple" Is Set

Posted on 2011-03-10
14
Medium Priority
?
333 Views
Last Modified: 2012-05-11
Hi,
I'm looking to see what others have done with the proverbial challenge of using the        <SELECT multiple> object and then looping over the comma delimited list to gather the user's choices when the <OPTION> values may also contain a comma. The <OPTION> values are dynamically set and established by the users, and they often have use for comma's in those values. Traditionally, I employ the replace function and do some behind the scenes things so it is seamless to the user.

It's too bad you can't set a delimiter property on the <SELECT> object. Anyway, what are some ways you handle these situations?

I'm developing for CF8 web apps and using CF Studio 4.5 with CF8 Homesite Plugins added in.

Cheers,
Ty
0
Comment
Question by:MedtronicVascSR
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
14 Comments
 
LVL 39

Expert Comment

by:gdemaria
ID: 35098979

Use cfloop to loop over the values with a diffrent delimiter

<cfloop index="aVal" list="#myList#" delimiter="|">
   <option value="#aVal#"> #aVal#</option>
</cfloop>


if that doesn't answer it, please show your code, it sounds a little odd to me..
0
 

Author Comment

by:MedtronicVascSR
ID: 35099210
Hi,
The only delimter allowed with <SELECT> is the comma. So, when you loop over it you have to use the comma. The programming challenge is that the values from the <SELECT> may actually have comma's as well thus giving you more items in your loop than were actually selected. Ideally, the <SELECT> object should be used with unique ID's in the value property and the human readable value displayed to the user, but this isn't always the situation.

For Example (values are not hardcoded by dyanamically generated from user maintained data):
<SELECT NAME="Other_Data" multiple size="25">
   <OPTION VALUE="Data Here"> Data Here
   <OPTION VALUE="Data There"> Data There
   <OPTION VALUE="Data Where"> Data Where
   <OPTION VALUE="Data, Anywhere"> Data, Anywhere
   <OPTION VALUE="Data Everywhere"> Data Everywhere
</SELECT>

<cfloop index="ctr" list="#Other_Data#">
   <cfoutput>
      * #ctr#<br>
   </cfoutput>
</cfloop>

Desired Output:
* Data Here
* Data There
* Data Where
* Data, Anywhere
* Data Everywhere

Actual Output (the comma in the list splits up the value, "Data, Anywhere"):
* Data Here
* Data There
* Data Where
* Data
* Anywhere
* Data Everywhere

Cheers,
Ty
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35099474
Ohhhh, you mean on a FORM POST...

All you need to do is clean your value so that you don't have any commas or spaces and such.  Typically, the value hold a numeric ID for the "other_data" which is linked to the foreign key table ...

<SELECT NAME="Other_Data" multiple size="25">
   <OPTION VALUE="1"> Data Here
   <OPTION VALUE="2"> Data There
   <OPTION VALUE="3"> Data Where
   <OPTION VALUE="4"> Data, Anywhere
   <OPTION VALUE="5"> Data Everywhere
</SELECT>


Alternatively, just use clean data for your value, you can even use numbers to represent the values.

<SELECT NAME="Other_Data" multiple size="25">
   <OPTION VALUE="DataHere"> Data Here
   <OPTION VALUE="DataThere"> Data There
   <OPTION VALUE="DataWhere"> Data Where
   <OPTION VALUE="DataAnywhere"> Data, Anywhere
   <OPTION VALUE="DataEverywhere"> Data Everywhere
</SELECT>

0
More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

 
LVL 52

Expert Comment

by:_agx_
ID: 35099502
#Other_Data#

...dyanamically generated from user maintained data


Generated and stored exactly how?  Are you storing one string in a column or are the values stored in separate records.  

OtherData Column
#1 -   "aa,bb,cc,dd"

.... versus

OtherData Column
#1 -  "aa"
#2 - "bb"
#3 - "cc"
....
0
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
ID: 35099508

Alternatively, you can add a | to the value, that will add | to the mix so that the list of values will be more like..

Data Here|,Data, Anywhere|,Data There|

This will allow you to loop with | as the delimiter, then just strip out the , if it's the first character.

<SELECT NAME="Other_Data" multiple size="25">
   <OPTION VALUE="Data Here|"> Data Here
   <OPTION VALUE="Data There|"> Data There
   <OPTION VALUE="Data Where|"> Data Where
   <OPTION VALUE="Data, Anywhere|"> Data, Anywhere
   <OPTION VALUE="Data Everywhere|"> Data Everywhere
</SELECT>

0
 
LVL 52

Expert Comment

by:_agx_
ID: 35099532
@gdemaria - I agree it's simple if the data's stored in separate records. But the code posted makes it appear that the source is just one big string list ....

      <cfloop index="ctr" list="#Other_Data#">
....
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35099581
> But the code posted makes it appear that the source is just one big string list ....

Yes, I was hoping to encourage using a seperate table.  My second example may help if it HAS to be that way..

I suspect we would both encourage normalization if it were possible in the authors system :)
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35099668
Believe me ... we're on the same page about using separate tables. It's *much* better.  But I've run into several people lately w/legacy apps that can't change the table.  But maybe that's not the case here?

@Medtronic - can you clarify your table structure for us? If at all at possible you should use an "ID" or separate table as gdemaria suggested.
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35099699
> Believe me ... we're on the same page about using separate tables

I had no doubt :)
0
 

Author Comment

by:MedtronicVascSR
ID: 35100954
Ok, lively dialogue here. Let me clarify a couple things.
1) first, the "dyanamically generated from user maintained data" is stored in a table as rows, and when the <select> object is created/set the <options> are looped over to fill the values. It's not one long string as assumed above.
2) Yes, using a unique is preferable as I also mentioned above, but not all situations can accomodate it. Sometimes you simply need to capture what the user wants the data or values to look like (with or without commas), then store them in another table "as is".

So, back to dealing with commas when looping over the selections after a Form post. I work around it already (with the replace function) and was curious how others prefer to deal with it. I like the one idea by gdemaria where you add a character of your choice at the end of each value to act as the new delimiter then strip away the comma. I do a similar thing when I have a few values to store in the Value property and separate each with a delimiter of my choosing.

Cheers,
Ty
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35102107
was curious how others prefer to deal with it.

I always put records in a table and give them a numeric record ID. Then use the "ID" as the <option> value, not the text.  That way commas and such are never a problem.  The text can be anything the users want. I don't worry about it because I'm using the record number instead.
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 35102173

I do the same as agx.  

It is inevitable that the user will want to change the text, and it would have to be updated in all records instead of in the one look-up table.
0
 

Author Comment

by:MedtronicVascSR
ID: 35109553
I completely agree, and when I design new systems that's the direction I go. In this case it's an older system that needed some upgrading and I needed to conform/work with the original design, despite its flaws  :-)
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35111649
I had a hunch this was a legacy system.  The good news is the data's stored in rows..  so you've got options.  I've seen (and inherited) much uglier apps ;-)  Since you can't change the structure, what you ended up with is fine IMO. It's the only simple work around I can think of.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
Today, I was working on some optimization and spam-stopping techniques when I encountered Ben Nadel's post to reduce spam feature using Math (http://www.bennadel.com/blog/197-How-I-Stop-Spammers-On-My-ColdFusion-Blog.htm). While this method is not o…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question