Solved

How do I make the first entry in a CFSELECT blank/empty?

Posted on 2012-03-14
12
1,008 Views
Last Modified: 2012-06-27
I am using the CFSELECT statement below to populate a drop-down box. I need the first choice to be left blank. Other than creating a blank or NULL option in the database, it is possible? I need to make the first option blank and the drop-down required. Below is what I am using - thank you.

 <cfselect name="TypeOfWidget"
             query="TypeOfWidget"
             value="TypeOfWidget"
             display="TypeOfWidget"
             selected="#VARIABLES.TypeOfWidget#">
   </cfselect>
0
Comment
Question by:earwig75
  • 5
  • 4
  • 3
12 Comments
 
LVL 39

Accepted Solution

by:
gdemaria earned 223 total points
ID: 37722106
Just do it manually instead..

<select name="typeofwidget"....>
    <option value="">Select One</option>
    <cfquery name="xxx">
        <option value="getQuery.value"> #getQuery.label#</option>
     </cfquery>
</select>
0
 

Author Comment

by:earwig75
ID: 37722172
What would I use for getQuery.value and getQuery.label in your example? I'm sorry I am a bit confused and should mention I am a beginner.
0
 
LVL 39

Assisted Solution

by:gdemaria
gdemaria earned 223 total points
ID: 37722229
All of the values you've shown are "typeOfWidget" .. basically the real values you would put into that CFSELET statement would go into these places.

<select name="typeofwidget"....>
    <option value="">Select One</option>
    <cfquery name="getData"
        <option value="#getData.widgetID#"> #getData.widgetName#</option>
     </cfquery>
</select>


Assuming your query is called getData and it fetches from widgets table which has a widgetID and a widgetName, you would do the above.

basically the VALUE has the value you want to pass in the form field when submitted and the LABEL (or widgetName in my example) is what you would show to the user..

If you had used real values in your statement, I could have shown you real values here..
0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 277 total points
ID: 37722465
basically the VALUE has the value you want to pass in the form field when submitted and the LABEL (or widgetName in my example) is what you would show to the user..

To elaborate, we assume most db tables have a unique numeric ID to go along with the user-friendly title (ie TypeOfWidget column)

WidgetID, TypeOfWidget
1, Widget A       <=== user friendly title
2, Widget B
3, Widget C
...

<cfselect
                       value="TypeOfWidget"
                       display="TypeOfWidget" ...

Right now you're using the title for both the "display" and "value".
         ie  Result is <option value="Widget A">Widget A</option>

But typically you should use the numeric id for the value instead
        ie <option value="1">Widget A</option>
0
 

Author Comment

by:earwig75
ID: 37723392
Thank you. So the value us what would get written to my master table? Would I then need to have additional code when viewing the record to say value 1 is widget A?
0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 277 total points
ID: 37723420
So the value us what would get written to my master table?

Yes.
       
Would I then need to have additional code when viewing the record to say value 1 is widget A?

We'd have to see the query, but most likely yes. This is a silly example, but say you insert the #form.TypeOfWidget# value into another table called "Orders".

      OrderID | OrderDate | Quantity | WidgetID
      1 | 3/15/2012 | 3 | 1       <=== 1 is Widget A

Then want to list the orders and names of the widgets. Yes, you'd need a JOIN to the widget table to get the "title" of the widget

      SELECT   orders.OrderID, orders.OrderDate, widgets.TypeOfWidget
      FROM    Orders  INNER JOIN Widgets ON widgets.WidgetID = orders.WidgetID
      WHERE  OrderID  = 123

.. which would give you

      OrderID | OrderDate | Quantity | TypeOfWidget[
      1 | 3/15/2012 | 3 | Widget A
0
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.

 

Author Comment

by:earwig75
ID: 37724516
Ok, I basically have 1 master table where I am storing all details of my record. The drop down I started this thread about is a list of things that I am populating the drop down with. After a record is created, it can be viewed on a .cfm page and edited again.

This is the query I am using for the drop down:

In my CFC:
 <cffunction name="GetSources"
             returntype="query"
             hint="Get Sources">

  <cfquery datasource="#datasourcea#"
           name="sources">
  SELECT OrderID, Source
  FROM Rtbl_Source
  ORDER BY OrderID
  </cfquery>
  <cfreturn sources>

 </cffunction>

On the page with drop-down:
 <cfinvoke component="origdb"
           method="GetSources"
           returnvariable="sources">

Am I doing this in a way that is not preferred? I believe I am storing the "source" in my master DB as the real value and not the "ID" which I am calling OrderID so I could order them easier on my page.

Thanks again for the help everyone.
0
 
LVL 39

Assisted Solution

by:gdemaria
gdemaria earned 223 total points
ID: 37724620
>  I believe I am storing the "source" in my master DB as the real value and not the "ID" which I am calling OrderID so I could order them easier on my page

Right, if you are storing the name of the source instead of the ID, you're not doing it correctly.

You want to store the ID of the source.   That way if your client wants to change the source name, it will immediately change everywhere.   This is the concept of "normalization" of a database.

Just another tip...   as your database grows with more tables, a convention to keep things clearer would be to name your ID and your primary column with similar bases..

Table:   Sources
Primary Key:  Source_ID
Name column:  Source    or SourceName

Table:  Category
Primary Key  Category_ID
Name: Category   or CategoryName

Then if when you want to associated these to a product:

Table:  Products
Primary Key:  Product_ID
Name:   ProductName
Category:   Category_ID   ---  foreign key to categories table
Source:   Source_ID   -- foreign key to sources table


That's a general example without knowing your particular design..
0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 277 total points
ID: 37724777
Am I doing this in a way that is not preferred?

It sounds that way.  Best to avoid duplicating string values in multiple tables because it typically leads to data integrity problems. If you change the value in one table, but not the others, your queries may break because the values no longer match.  Storing the string titles also requires mores storage (for data and indexes), and can be slightly less slower in terms of query speeds. But data integrity problems is the more important reason.

so I could order them easier on my page.

You can still do that with a JOIN. By joining on the numeric id you have access to the columns in both tables and can sort by the "title", instead of the numeric id. Using the earlier example

      SELECT   orders.OrderID, orders.OrderDate, widgets.TypeOfWidget
      FROM    Orders  INNER JOIN Widgets ON widgets.WidgetID = orders.WidgetID
      WHERE  orders.OrderID  = 123
      ORDER BY widgets.TypeOfWidget
0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 277 total points
ID: 37724811
Oh snap. Didn't see there was already a response to that one ...

<ot>
Really not lovin the new EE layout. One response blurs into the next.  I'm going snow blind, lol.  It's "cute" but the old layout with it's different colors for asker, expert, self was more intuitive. Better UI.
</ot>
0
 
LVL 39

Assisted Solution

by:gdemaria
gdemaria earned 223 total points
ID: 37724849
> Really not lovin the new EE layout

agreed... there are several step backwards including how the code box is applied and appears..
not many steps forward.  I think they just switched developers and the new team wanted to start over.
0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 277 total points
ID: 37724955
Even navigating is cumbersome. The lack of back buttons, or remembering menu options, on most screens is bizzare. The old site was too heavy on the ajax imo, but boy I'd kill for it now. Too much "cutesy" stuff and not enough functionality. They need to turn around, because they're going the wrong way ..
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
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 …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

867 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

16 Experts available now in Live!

Get 1:1 Help Now