Reputation: 3200
I have a drop down list that is generated by two loops. The inner loop generates a series of numbers ie from 0 to 23. The outer loop, is a query loop that selects the correct value from the 23 numbers based on the values stored in my database.
My issue here is that those two loops conflict that results in displaying the numbers from 0 to 23 twice. How can keep both loops but avoiding this issue? This problem also cause issues when the form is submitted by trying to submit the form twice and deleting the user's input.
This is my code:
<select id="openHours#CountVar#" name="openHours#CountVar#">
<cfloop query="doctorHours" >
<cfloop from="0" to="23" index="OpenHours">
<option value="#openHours#"
<cfif TimeFormat(doctorHours.openTime,'HH') EQ OpenHours AND CountVar EQ doctorHours.day > selected="selected"</cfif>>#OpenHours#</option>
</cfloop>
</cfloop>
</select>
This is my CFDUMP for that query
query
RESULTSET
query
CLOSETIME DAY DOCTORID OPENTIME
1 1970-01-01 16:00:00.0 4 2011041516290025478779 1970-01-01 10:00:00.0
2 1970-01-01 16:00:00.0 1 2011041516290025478779 1970-01-01 13:00:00.0
3 1970-01-01 16:00:00.0 2 2011041516290025478779 1970-01-01 13:00:00.0
CACHED false
EXECUTIONTIME 0
SQL select doctorID, opentime, closetime, day from doctorBusinessHours where doctorID='2011041516290025478779'
Upvotes: 0
Views: 556
Reputation: 13655
You should return only the hours you need and then loop for the dropdownlist creation:
DATEPART(hh,yourdate)
will return the hours for your datetime value:
<cfquery name="doctorHours" datasource="#ds#">
SELECT doctorID,DATEPART(hh,openTime) As OpenHours, DATEPART(hh,closetime) As CloseHours
FROM doctorHours
WHERE day = #CountVar#
AND doctorID='#docID#'
</cfquery>
ValueList
will transform your query results into a list:
<cfset openTimesList = ValueList(doctorHours.OpenHours) />
<cfset closeTimesList = ValueList(doctorHours.CloseHours ) />
ListContains
will return the index of the value within your list:
<select id="openHours#CountVar#" name="openHours#CountVar#">
<cfloop from="0" to="23" index="OpenHours">
<option value="#openHours#"
<cfif ListContains(openTimesList,OpenHours) NEQ 0 >
selected="selected"
</cfif>
>#OpenHours#</option>
</cfloop>
</select>
You can use the same strategy for the closeTimesList.
Upvotes: 2
Reputation: 7193
Hmmm....the number of values displayed in the code above will be equivelant to the number of records returned by the query X 23. If you're query returns 2 records you will see 46 options and so on. It seems like you believe the query has only 1 record. I would suggest perhaps it has more.
Try LIMIT 1 or TOP 1 in your query - or use Maxrows (as suggested in the comments)... but make sure you know what you are including and what you are excluding. You need to know why your query is not what you expect :)
Upvotes: 1