Greg's Blog

helping me remember what I figure out

Populating a Drop Down Box From a Comma Delimited Field

| Comments

Occasionally you’ll come across database fields that contains a list of keywords that are comma delimited. One instance would be for storing the style of a certain product. Take shoe as an example, you can have a trainer, walking, formal or work style for said shoe. These different styles in our case were stored in one field in database. However when it came to building the front end of the shopping cart we needed to allow the user to choose a specific style for said shoe. In the following I’ll be explaining how populate a drop down box from a comma list of items stored in a database.Step 1, get the items of interest. Here we right a basic query that retrieves the contents of the field we wish to display, in our case the style of a shoe. The code below shows you how to do this: [code] select table.style from table where item = ‘shoe’ [/code] Here we select the content of a field called style in a table called table where the item value is equal to ’shoe’. Next we define a variable and store the results of aforementioned query in that variable. [code] [/code] As result of this the variable keywords contains the following information (based on the above example: trainer, walking, formal, work. Now we need to extract that information and put into a drop down menu. To this end we will have to loop over that variable and display each and every item in an field. Thankfully allows you to specify a delimiter and in our case it would be a ‘,’. So the entire Cold Fusion code would look something like this: [code] [/code] And there you go, you know have broken down a comma delimited field into a drop down menu based on the content of a field in your database.