Skip to main content
Inspiring
February 9, 2011
Question

Populating drop down menu with iniitials if checkbox is checked in database

  • February 9, 2011
  • 2 replies
  • 1234 views

I am trying to populate a drop down menu with a user's initials that I have in my database, but it has to depend if the "Received_By_Initials" column is checked. I tried putting an If statement in the loop query on the drop down, but it doesn't seem to work. This code I'm using is on an edit page, so that's why I have the selected code on here too because I would like to have the initials display in the drop down if they are already chosen from before. The reason I want to do this is because I have a lot of different drop downs I'm going to use these initials for and I don't want to create multiple tables for Received By, Assigned By, RFQ Leader, etc. Does anyone know how to fix this so this will work? Here's the code:

<CFQUERY NAME="ShowReceivedByInitials" Datasource="#application.DataSource#">
SELECT Initials, Received_By_Initials
From Users
Order by Initials
</CFQUERY>

<select name="Initials">
<option value="">Select Initials</option>
<cfloop query="ShowReceivedByInitials">
<option value="#Initials#"
<cfif #ShowReceivedByInitials.Received_By_Initials# EQ "1">selected</cfif>>#Initials#
</cfloop>
</select>

Thanks.

Andy

    This topic has been closed for replies.

    2 replies

    Inspiring
    February 9, 2011

    What you appear to have so far is that the last record where the user's initials are "1" will cause the item to be selected.  That's probably not what you want.

    Something that is not clear to me is the concept of something being checked in a database.

    BKBK
    Community Expert
    Community Expert
    February 9, 2011

    <cfoutput query="ShowReceivedByInitials">
        <cfif ShowReceivedByInitials.Received_By_Initials EQ 1>
        <option value="#ShowReceivedByInitials.Initials#">#ShowReceivedByInitials.Initials#</option>
        </cfif>
    </cfoutput>

    However, you should realize your design could be troublesome. People could share the same initials.

    Inspiring
    February 9, 2011

    Thank you. I had to change a few things in the code to get it to work, but I still don't know how to get the drop down to have the correct initials be selected when the page loads. Here's how the code is working with just getting the initials to be populated correctly. For the other response I received, what I'm talking about with something being checked is I have a Users table with the user's name, iniitials, etc. There are also columns that are check boxes for if the user belongs in certain groups for certain drop down menus or not such as Received By, Assigned By, or RFQ Leader.

    <select name="Initials">
    <cfloop query="ShowReceivedByInitials">
    <cfif ShowReceivedByInitials.Received_By_Initials EQ 1>
    <option value="#ShowReceivedByInitials.Initials#">#ShowReceivedByInitials.Initials# </option>
    </cfif>#Initials#
    </cfloop>
    </select>

    How do I fix this now so that the correct initials are selected and displayed in the drop down when the page loads? I tried this, but it doesn't select the correct initials:

    <select name="Initials">
    <cfloop query="ShowReceivedByInitials">
    <cfif ShowReceivedByInitials.Received_By_Initials EQ 1>
    <option value="#Initials#"></cfif><cfif #ShowReceivedByInitials.Initials# EQ #ShowItem.Received_By#>selected</cfif>#Initials#</option>
    </cfloop>
    </select>

    Thanks.


    Andy

    Inspiring
    February 9, 2011

    I usually do what you seem to be describing like this:

    cfquery name = "bigquery"

    select all possible records

    cfquery name = "littlequery"

    select just the record I want

    <cfselect

    query="bigquery"

    value="something from bigquery"

    display="something else from bigquery"

    selected = "something from littlequery">

    Which leads to, what is ShowItem.Received_By?