Solved

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

Posted on 2011-03-10
14
326 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

PROBLEM: How to add your own buttons to the bottom toolbar with paging info ( result count ). While creating a cfgrid, I ran into an issue where I wanted to embed my own custom buttons where the default ones ( insert / delete / etc… ) are for aes…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now