10 Replies Latest reply: Jun 25, 2012 3:57 PM by goodychurro1 RSS

    How can I loop instead of hardcoding this info

    goodychurro1

      Hi all

      I have the following code, how can I cfloop and dynamically output this info instead of having to have each meeting time? Thanks

       

      <cfselect name="MEETINGTIME" width="90" label="Time"onchange="UsersGrid.dataProvider.editField(UsersGrid.selectedIndex, 'MEETINGTIME', MEETINGTIME.selectedItem.data);">

                              <option value="None">None</OPTION>

                                  <option value="05:30 AM">05:30 AM</option>

                                  <option value="06:00 AM">06:00 AM</option>

                                  <option value="06:30 AM">06:30 AM</option>

                                  <option value="07:00 AM">07:00 AM</option>

                                  <option value="07:30 AM">07:30 AM</option>

                                  <option value="08:00 AM">08:00 AM</option>

                                  <option value="08:30 AM">08:30 AM</option>

                                  <option value="09:00 AM">09:00 AM</option>

                                  <option value="09:30 AM">09:30 AM</option>

                                  <option value="10:00 AM">10:00 AM</option>

                                  <option value="10:30 AM">10:30 AM</option>

                                  <option value="11:00 AM">11:00 AM</option>

                                  <option value="11:30 AM">11:30 AM</option>

                                  <option value="12:00 AM">12:00 AM</option>

                                  <option value="12:30 AM">12:30 AM</option>

                                  <option value="01:00 AM">01:00 AM</option>

                                  <option value="12:00 PM">12:00 PM</option>

                                  <option value="12:30 PM">12:30 PM</option>

                                  <option value="01:00 PM">01:00 PM</option>

                                  <option value="01:30 PM">01:30 PM</option>

                                  <option value="02:00 PM">02:00 PM</option>

                                  <option value="02:30 PM">02:30 PM</option>

                                  <option value="03:00 PM">03:00 PM</option>

                                  <option value="03:30 PM">03:30 PM</option>

                                  <option value="04:00 PM">04:00 PM</option>

                                  <option value="04:30 PM">04:30 PM</option>

                                  <option value="05:00 PM">05:00 PM</option>

                                  <option value="05:30 PM">05:30 PM</option>

                                  <option value="06:00 PM">06:00 PM</option>

                                  <option value="06:30 PM">06:30 PM</option>

                                  <option value="07:00 PM">07:00 PM</option>

                                  <option value="07:30 PM">07:30 PM</option>

                                  <option value="08:00 PM">08:00 PM</option>

                                  <option value="08:30 PM">08:30 PM</option>

                                  <option value="09:00 PM">09:00 PM</option>

                                  <option value="09:30 PM">09:30 PM</option>

                                  <option value="10:00 PM">10:00 PM</option>

                                  <option value="10:30 PM">10:30 PM</option>

                                  <option value="11:00 PM">11:00 PM</option>

                                  <option value="11:30 PM">11:30 PM</option>

                                </cfselect>

        • 1. Re: How can I loop instead of hardcoding this info
          Dan Bracuk Community Member

          Use cfloop condition = "YourDateTime lte #MaxDateTime#"

           

          inside your loop use DateAdd to increment YourDateTime and use TimeFormat to display it.

          • 2. Re: How can I loop instead of hardcoding this info
            goodychurro1 Community Member

            Like this Dan? Thanks

             

            <cfset YourDateTime = "7:30AM">
            <cfset MaxDateTime = "11:30PM">

            <cfloop condition = "YourDateTime lte #MaxDateTime#">
            #timeFormat(dateAdd('n', 30, YourDateTime),'hh:mm:ss tt')#
            </cfloop>

            • 3. Re: How can I loop instead of hardcoding this info
              Dan Bracuk Community Member

              Did it work when you ran it?

              • 4. Re: How can I loop instead of hardcoding this info
                goodychurro1 Community Member

                When I add the code I get a nasty screen of death, when I take it away the error goes. Error:

                 

                IIS 7.5 Detailed Error

                 

                HTTP Error 500.0 - Java heap space
                The page cannot be displayed because an internal server error has occurred.

                • 5. Re: How can I loop instead of hardcoding this info
                  Dan Bracuk Community Member

                  That's probably because you have an infinite loop.   Also, you are setting your two variables to strings instead of datetime objects.  This might cause unexpected results.

                  • 6. Re: How can I loop instead of hardcoding this info
                    -==cfSearching==- Community Member

                    goodychurro1 wrote:

                     

                    #timeFormat(dateAdd('n', 30, YourDateTime),'hh:mm:ss tt')#

                     

                    That does not change the value of the #YourDateTime# variable. So the loop just runs until it crashes because

                    the value of #YourDateTime# is always less than 11:30PM.  You need to increment the value on each loop and assign the new value to #YourDateTime#.

                     

                            <cfloop ....>

                                    ...

                                    <cfset YourDateTime = dateAdd('n', 30, YourDateTime)>

                            </cfloop>

                    • 7. Re: How can I loop instead of hardcoding this info
                      goodychurro1 Community Member

                      Hi Dan, thanks I tried this it is almost working, I get all the times in half hour intervals from 8 - 5.30 but in the wrong date!! {ts '1899-12-30 08:00:00'}

                       

                      <cfset YourDateTime = "7:30AM">
                      <cfset MaxDateTime = "11:30PM">

                      <cfset startweek_dt = TimeFormat(YourDateTime, "hh:mm") >
                      <cfloop index="ii" from="1" to="20">
                        <cfset startweek_dt = DateAdd('n',30, startweek_dt)>
                        <cfoutput>#startweek_dt#</cfoutput><br>
                        </cfloop>

                      • 8. Re: How can I loop instead of hardcoding this info
                        goodychurro1 Community Member

                        Thanks cfsearching, I don't understand where I use YourDateTime variable if it is inside the cfloop, I tried using the variable in the to and from parts of cfloop but I kept getting errors

                         

                        <cfset YourDateTime = "7:30AM">
                        <cfset MaxDateTime = "11:30PM">

                        <cfloop condition = "YourDateTime lte #MaxDateTime#" >
                        #timeFormat(dateAdd('n', 30, YourDateTime),'hh:mm:ss tt')#
                        <cfset YourDateTime = dateAdd('n', 30, YourDateTime)>
                        </cfloop>

                        • 9. Re: How can I loop instead of hardcoding this info
                          -==cfSearching==- Community Member

                          I get all the times in half hour intervals from 8 - 5.30 but in the wrong date!! {ts '1899-12-30 08:00:00'}

                           

                          It is not the wrong date.  Since you only specified a time (ie "07:30AM") the "date" portion defaults to 0 or 12/30/1899 .  If you needed a specific time and date, you must specify it when creating the variables. For example:

                           

                                <cfset YourDateTime = createDateTime(2012, 6, 25, 7, 30, 0)> ... or

                                <cfset YourDateTime = parseDateTime("2012-06-25 07:30AM")>

                           

                          BUT .. in your case it does not make a difference because you are not using the date for anything. So just use timeFormat() to display the time only.

                           

                               ie     #timeFormat( yourVariableName,  'hh:mm:ss tt')#

                          • 10. Re: How can I loop instead of hardcoding this info
                            goodychurro1 Community Member

                            Thanks cfsearching, this code works great for outputting the times:

                             

                            <cfset YourDateTime = "7:00AM">

                            <cfset startweek_dt = #timeFormat( YourDateTime,  'hh:mm tt')# >
                            <cfloop index="ii" from="1" to="23">
                              <cfset startweek_dt = DateAdd('n',30, startweek_dt)>
                              <cfoutput>#timeFormat( startweek_dt,  'hh:mm tt')#</cfoutput><br>
                              </cfloop>

                             

                            This code works great for outputting them in a cfselect:

                             

                            <cfset YourDateTime = "7:00AM">

                            <cfset startweek_dt = #timeFormat( YourDateTime,  'hh:mm tt')# >
                            <option value="None">None</OPTION>
                            <cfloop index="ii" from="1" to="23">
                              <cfset startweek_dt = DateAdd('n',30, startweek_dt)>
                                                        <option value="<cfoutput>#timeFormat( startweek_dt,  'hh:mm tt')#</cfoutput>"><cfoutput>#timeFormat( startweek_dt,  'hh:mm tt')#</cfoutput></option>
                              </cfloop>