Solved

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

Posted on 2011-03-10
14
328 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 125 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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 …
CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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