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

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

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

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
earwig75
Asked:
earwig75
  • 5
  • 4
  • 3
9 Solutions
 
gdemariaCommented:
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
 
earwig75Author Commented:
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
 
gdemariaCommented:
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
Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

 
_agx_Commented:
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
 
earwig75Author Commented:
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
 
_agx_Commented:
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
 
earwig75Author Commented:
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
 
gdemariaCommented:
>  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
 
_agx_Commented:
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
 
_agx_Commented:
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
 
gdemariaCommented:
> 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
 
_agx_Commented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

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