0
votes

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. :)

3
You're missing a cfif close tag.Josh Siok
that's not my issue though :)Geo
@JoshSiok probably I left it out when I was trying to make my code look pretty, I will fix it. ThanksGeo
It would be helpful to know what the doctorHours object is. My assumption is that it is a query object. In that case, you're probably only comparing the first record in the object and would need to loop through all of the times. Add more details if you can.Josh Siok
The doctorHours is a query that pull out data from my table which in this case is an id with an "openHour" and "closeHour" option.Geo

3 Answers

4
votes
<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.

3
votes

I think this may be what you need.

<option value="#OpenHours#" <cfif doctorHours.openTime EQ OpenHours>selected="selected"</cfif> >#openHours#</option>
1
votes

Is this what you're looking for?

<select name="openHours#CountVar#">
    <cfoutput query="doctorHours">
       <cfif doctorHours.openTime neq ''>
          <option name="#TimeFormat(doctorHours.openTime)#">#TimeFormat(doctorHours.openTime)#</option>
       </cfif>
    </cfoutput>
</select>