Solved

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

Posted on 2011-03-10
14
331 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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