Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / ColdFusion / General CF Topics / July 2008



Tip: Looking for answers? Try searching our database.

Looping with cfif

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dwmart - 25 Jul 2008 01:28 GMT
I am trying to create a calender to book time slots per 15 minute intervals. My
problem arises when I want to block the booking of a time slot if the time slot
has already been taken. What my code is doing is blocking out the very first
time slot of any loop that contains an already booked slot. Obviously that is
not good because it needs to only block the specific time slot/s that have been
booked.

I am learning as I go with this project but would appreciate any pointers if
you have some :)

<cfloop from="#startTime#" to="#endTime#" index="i"
step="#CreateTimeSpan(0,0,m,0)#">
                      <cfoutput>
                        <table
                          <tr>
                            <td > #TimeFormat(i, "h:mm tt")#

         <cfif Recordset2.vTime EQ #TimeFormat(i, "h:mm tt")#>
                                Time Already Booked
                             
                                <cfelse>

   <a href="addresss">Book Time Slot</a>
                             
</cfif>
</td>
                          </tr>
                        </table>
                        <br />
                      </cfoutput>
                    </cfloop>
Dan Bracuk - 25 Jul 2008 04:44 GMT
I'd be looking to solve this with my database design.  Specifically, I'd have a block table with a block_id, start_datetime, end_datetime, and other fields I think I might need.
dwmart - 25 Jul 2008 17:59 GMT
I am not sure how that would work. If there is a record it in the database for
that particularly time there needs to have a message that the time slot is
booked, if it doesn't have a record it would have a link to book the time slot.


The code works well, except it stops looking for the "booked" time slots after
the very first positive. I just need to know how to get coldfusion to continue
checking each time between the starttime and endtime that the loop creates.
There are so many things that come into play I dont think I can do it by
database structure alone.
CF_output - 25 Jul 2008 18:48 GMT
Your immediate problem is that the variable "Recordset2.vTime" is remaining the
same... so it will only be able to match one time slot that is booked.

Your are only looping over the times and not your recordset.

For example, if the value of Recordset2.vTime is 3:15... your current loop
value will always be "3:15", so it can only match one time.

In addition to your current loop, you will need to use an output query, list,
or loop against your recordset to compare against your times.
dwmart - 27 Jul 2008 17:35 GMT
Thank you, I had guessed this but just don't really know how to go about it.
-==cfSearching==- - 27 Jul 2008 20:04 GMT
Assuming your reservations cannot span multiple days, the database approach may
be simpler.  You could create a table containing fifteen minute time intervals.

TimeSlot
00:00:00
00:15:00
00:30:00
00:45:00
01:00:00
01:15:00
...
23:45:00

Then use an OUTER JOIN to retrieve the time slots needed and return a value
indicating whether the slot is already reserved or not.  Finally, output the
query.  No extra looping required.  

The exact syntax depends on your database and the types of your "startTime"
and "endTime" columns.  I could provide more specific information if you post
the structure of the table containing the "booked" slots and some sample
values.
dwmart - 27 Jul 2008 20:23 GMT
The thing about this is that there are could be hundreds of people to book time
slots with and they all have different days, different "office hours" and
different length of appointment times. I am defiantly not an database design or
coldfusion expert. This is the best way I could figure out. It seems like
everyone is suggesting its better to solve in the database than Coldfusion but
I really wish it could be in Coldfusion :)

Is there any way to have the cfif statement continually check against the
looping time slots from startTime to endTime?

<table width="100%" border="0">
                  <tr>
                    <td><cfloop from="#startTime#" to="#endTime#" index="i"
step="#CreateTimeSpan(0,0,m,0)#">
                      <cfoutput>
                        <table width="711" border="0" align="center"
cellpadding="1">
                          <tr>
                           <td > <cfif #Recordset2.AppHour# EQ #TimeFormat(i,
"h:mm tt")# AND #Recordset2.AppDay# EQ #URL.Day# AND #Recordset2.AppMonth# EQ
#URL.Month# AND #Recordset2.AppYear# EQ #URL.Year#>
                               #TimeFormat(i, "h:mm tt")#&nbsp; Already Booked
                             
                                <cfelse>
              #TimeFormat(i, "h:mm tt") <a
href="book.cfm?year=#URL.Year#&amp;month=#URL.Month#&amp;day=#URL.day#&amp;curre
nthour=#TimeFormat(i,"h:mm tt")#">Book Time Slot</a>
                              </cfif></td>
                             
                          </tr>
                        </table>
                        <br />
                      </cfoutput>
                    </cfloop></td>
                  </tr>
                </table>
-==cfSearching==- - 27 Jul 2008 22:00 GMT
> The thing about this is that there are could be hundreds of people to book
time slots with and
> they all have different days, different "office hours" and different length
of appointment times.

IMO, that sounds like all the more reason to use a database driven approach.  
It is likely to be more flexible and reusable than hard coding. However, it
does take some thought and planning.

> Is there any way to have the cfif statement continually check against the
looping time
> slots from startTime to endTime?

CF_Output already mentioned most of the options:  [i]..use an output query,
list, or loop against your recordset to compare against your times.[/i].  Each
has varying degrees of efficiency and accuracy.  Except for the list option, it
would probably require a nested loop.  Meaning for each time slot you would
loop through your query results, testing each record for a match. Not the most
efficient method, but here is psuedo code example.

<cfloop from="#startTime#" to="#endTime#" index="i"
step="#CreateTimeSpan(0,0,m,0)#">
   ...    
   <cfset isTimeSlotBooked  = false>
   <cfloop query="Recordset2">
       Compare the current value in the query against the current time slot
       If time slot is booked, set a flag and exit

       <cfif ATimematchWasFound>
           <cfset isTimeSlotBooked  = true>
           <cfbreak>
        </cfif>
    </cfloop>

    <cfif isTimeSlotBooked >
        Already booked
    <cfelse>
        Not booked
    </cfif>

</cfloop>

Another option is to use a QoQ.

<cfloop from="#startTime#" to="#endTime#" index="i"
step="#CreateTimeSpan(0,0,m,0)#">

        <!--- within each loop check your query to see if it contains
           a booking for the current time slot --->
        <cfquery name="checkTime" dbtype="query">
            SELECT  Something
            FROM     Recordset2
            WHERE ..... your conditions here ...
        </cfquery>

        <cfif checkTime.recordCount GT 0>
             reserved
         <cfelse>
             not reserved
        </cfif>
</cfloop>
dwmart - 31 Jul 2008 04:22 GMT
Thank you ... your reply was very helpful :)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.