User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 392,085 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,951 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1356 | Replies: 2 | Solved
Reply
Join Date: Oct 2006
Posts: 71
Reputation: assgar is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Help Schedule Repeat events

  #1  
Jul 22nd, 2007
Schedule Repeat events
Hi

I am stuck in fact I don't know where to start, can you point
me in the right direction?

I have the form to capture the user input but don't
know how to proceed to use this information.



I need to allow the user to schedule repeated events for
the same time, for the same day between two time periods.
Ideally it would be great to check for conflicts.


Note: The info recorded by the form is listed below.

A) The start date and time is determine by the first event

B) The user selects a comdination of 1) interval 2) frequency and
3) week day to determine when the repeating event will occur:

1) interval:
Daily,
Weekly,
Monthly,
Annually
2) frequency:
Every
Every Other
Every 3rd
Every 4th
Every 5th
Every 6th


3) Week Day:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

C) The end date (YYYYMMDD) is then selected.




Note: I have the code to insert the results into the mysql database using PHP.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Location: New Jersey
Posts: 1,422
Reputation: stymiee is on a distinguished road 
Rep Power: 5
Solved Threads: 34
Moderator
Staff Writer
stymiee's Avatar
stymiee stymiee is offline Offline
He's No Good To Me Dead

Re: Schedule Repeat events

  #2  
Jul 22nd, 2007
You'll need to run a cron job periodically to handle the schedule tasks. You should check for the conflicts when the users enters the scheduled task. You definitely don't want to wait until the tasks are going to run to do this.
John Conde
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
Reply With Quote  
Join Date: Oct 2006
Posts: 71
Reputation: assgar is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
assgar assgar is offline Offline
Junior Poster in Training

Solution solution

  #3  
Jul 28th, 2007
Hi
Thanks for the assistance your effort was appreciated.
The final code can be found below.
This bit of code insert events into a database with these functionalities:
1) Single or multiple time blocks
2) Repeat event booking
3) Checks for event time conflicts
Note: if single a event is being booked message is sent to user
else if multiple repeat booking conflicts are store in
table for user review and change event to another day or time.
I have only provided a quarter of the code so I hope I have not missed
anything related to this topic.
<?php 
 
 
   /**
    Note: form provide these parameters:
     $start_date
     $new_interval
     $new_frequency
     $end_date  //if no end_date use start date
     $time_block //each time block is 10 or 15 min, this indicates the number of blocks 1 to 10
 
    
    Note: determine settings manually or use database stored configuration  
     $double_book == "Y"; //event double booking
 $repeat == "no";//if no end_date use start date and repeat is no else repeat is yes
    **/
 /*******Note:
        - array repeatEvent(int $startTime, str $interval, int $frequency, int $endTime)
       returns array of UNIX times
    - $startTime and $endTime must be valid UNIX time integer values
    - $interval must be (case-insensitive): 'day', 'week', 'month', or 'year'
    - $frequency must be positive integer (1 = every, 2, = every other, 3 = every 3rd, 4 = every 4th. 5 = every 5th, 6 = every 6th)
 *********/
 function repeatEvent($startTime, $interval, $frequency, $endTime)
    {
     //make sure all paramters are valid
     $startTime = (int) $startTime;
     $endTime = (int) $endTime;
  if($startTime == 0)
      {
               user_error("repeatEvent(): invalid start time");
          return(FALSE);
       }
     
     if($endTime < $startTime)
       {
           user_error("repeatEvent(): invalid end time");
        }
            
     $interval = strtolower(trim($interval));
     if(!in_array($interval, array('day','week','month','year')))
       {
                 user_error("repeatEvent(): Invalid interval '$interval'");
                  return(FALSE);
           }
     $frequency = (int)$frequency;
    if($frequency < 1)
       {
                   user_error("repeatEvent(): Invalid frequency '$frequency'");
                   return(FALSE);
       }
     $schedule = array();
     for($time = $startTime; $time <= $endTime; $time = strtotime("+$frequency $interval", $time))
        {
              $schedule[] = $time;
        }
     return($schedule);
   }

     /**-----------------------insert appointment-----------------------**/
     $sched = repeatEvent(strtotime($start_date), $new_interval, $new_frequency, strtotime($end_date));
   
     //outer loop repeated inserts
     foreach($sched as $date)
         {
      
       //inner loop the number of time blocks
       for($i = 0, $eTime = strtotime($event_time); $i < $time_block; 
           $i++, $eTime = strtotime("+$event_length minutes", $eTime)) 
    { 
      $new_event_time = date('H:i', $eTime); //increment time for new single or multi block event 
   $new_event_date = date('Y-m-d', $date);//increment date for single or repeat event
     
   /**-----------------------check conflicts------------------------**/
      //determine the number of appointments for date and time to determine conflicts
   $event_count = "SELECT count(event_id) 
     FROM cal_appointment
     WHERE event_date = '$new_event_date'
     AND event_time = '$new_event_time '";
     $count = mysqli_query ($mysqli, $event_count);
     while($row = mysqli_fetch_array($count))
      {
       list($book_count) = $row;
      }       
    
    /**----------- tables for inserting information-----------**/
    /**Note: I have removed table fields to simplify example **/
    
    //insert if conflict insert in to cal_repeat_conflict
    $conflict_query = "INSERT INTO cal_repeat_conflict()
          VALUES()";
       
   //no conflicts insert into cal_appointment table 
   $cal_query = "INSERT INTO cal_appointment()
         VALUES();
    
   /**------------------------evaluate event time conflict---------------------**/
   //check to ensure that only allowed number of bookings for time slot is  entered
   if($double_book == "N" && $book_count == 1 && $repeat == "no")
          {
        event_conflict_message();//event time conflict assigned
           }
              elseif($double_book == "Y" && $book_count == 2 && $repeat == "no")
           {
          event_conflict_message();//event time conflict assigned
    }
    elseif($double_book == "N" && $book_count == 1 && $repeat == "yes")
          {
        //insert into table event if time conflict
     mysqli_query($mysqli, $conflict_query)or die(mysqli_error($mysqli));
          }
              elseif($double_book == "Y" && $book_count == 2 && $repeat == "yes")
            {
         //insert into table event if time conflict
        mysqli_query($mysqli, $conflict_query)or die(mysqli_error($mysqli));
     }       
     else
         {
            //no conflicts insert into cal_appointment table 
            mysqli_query($mysqli, $cal_query)or die(mysqli_error($mysqli));
               }
        }//end for loop
         }//end foreach loop
?>
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 12:29 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC