Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

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

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
MedtronicVascSR
Asked:
MedtronicVascSR
  • 6
  • 5
  • 3
1 Solution
 
gdemariaCommented:

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
 
MedtronicVascSRAuthor Commented:
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
 
gdemariaCommented:
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
How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

 
_agx_Commented:
#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
 
gdemariaCommented:

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
 
_agx_Commented:
@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
 
gdemariaCommented:
> 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
 
_agx_Commented:
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
 
gdemariaCommented:
> Believe me ... we're on the same page about using separate tables

I had no doubt :)
0
 
MedtronicVascSRAuthor Commented:
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
 
_agx_Commented:
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
 
gdemariaCommented:

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
 
MedtronicVascSRAuthor Commented:
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
 
_agx_Commented:
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now