Search code examples
coldfusiondrop-down-menucoldfusion-9

Drop down lists in ColdFusion 9


I am trying to create a drop down list that incorporates database data and a custom loop at the same time. I need the list to show as "selected" the data coming from the database but also to allow the user to change it if needed with other data on the list.

More specifically, this is what I have so far:

<select name="openHours#CountVar#">
    <cfloop from="0" to="23" index="OpenHours">  
        <option value="#OpenHours#"><cfif doctorHours.openTime neq ''>#TimeFormat(doctorHours.openTime)#<cfelse>#OpenHours#</cfif></option>
    </cfloop>
</select>

The problem with this code is when a value is actually pulled from the database the rest of the value options are disappearing because #OpenHours# is isolated and I can have only one or the other. Also the value should be dynamic as well to get in the correct value in case of an update.

Can I incorporate this part

<cfif doctorHours.openTime neq ''>
    #TimeFormat(doctorHours.openTime)#
 <cfelse>
     #OpenHours#
 </cfif>

.. in a "selected" option somehow in order to have the correct value showing on my drop down list and also be able to press on the list and choose another option to update my time table if needed?

Adding more info on what I need:

I have a db table with 3 columns in it. An ID, openHours, and closeHours. What I am trying to achieve is: Create a drop down list that has hours from 0 to 23. Then check my db, if the openHours, and closeHours are not empty, get those values and have them as the pre-selected values on the drop down list. If my doctor opens at 9am and close at 6pm the the two drop down lists will have selected 09 and 18 and also allow me to choose another option if I want to update them. So i guess I need to check my db table and if my data match with any of the data in the drop down list, make that selection as the selected on.

hope that makes more sense now. :)


Solution

  • <select name="openHours#CountVar#">
        <cfloop from="0" to="23" index="OpenHours">  
            <option value="#OpenHours#"<cfif TimeFormat(doctorHours.openTime,'H') EQ    OpenHours> selected="selected"</cfif>>#OpenHours#</option>
        </cfloop>
    </select>
    

    This should give you what you're looking for. It loops through the list 0 to 23 and displays each one. If the value being displayed matchs doctorHours.openTime then it will show as selected.