Best practice for PHP/MySQL Appointment/Booking system

后端 未结 2 638
面向向阳花
面向向阳花 2021-02-02 04:58

I need some people to battle a \"best practice\" for a PHP/MySQL appointment system for a hairdresser I\'m currently working on. Hopefully, together we can clear some things up

2条回答
  •  隐瞒了意图╮
    2021-02-02 05:22

    In the end I went for a system that generated timestamps for the start and end dates in the database. While checking I added one second to the start and subtracted one second from the end to avoid any overlapping time for appointments.

    What did I end up doing

    Obviously I'm not sure this is the best practice, but it did work for me. The user starts by selecting their sex and a preference for the day. This sends an AJAX request to get the available personel and various kinds of appointment types (f.e. coloring hair, cutting hair, etc).

    When all settings have been choosen (sex, date, personel and type) I start by some simple validations: checking the date, checking if the date("N") is not 7 (sunday). If everything is okay, the more important stuff is started:

    1) The appointment type is fetched from the database including the total amount of time this type takes (30 minutes, 45 minutes, etc) 2) The available personal is fetched (a complete list of people on that day or just a single person if one is chosen) including their available times

    The personel (or one person) is then looped, starting with their own starting time. At this point I have a set of data containing:

    $duration (of the appointment type)
    $startTime (starting time of the person selected in the loop)
    $endTime (= $startTime + $duration)
    $personStart (= starting time of the person)
    $personEnd (= end time of the person)
    

    Let's take this demo data:

    $duration = 30 min
    $startTime = 9.00h
    $endTime = 9.30h
    $personStart = 9.00h
    $personEnd = 12.00h
    

    What I'm doing here is:

    while( $endTime < $personEnd )
    {
        // Check the spot for availability
        $startTime = $endTime;
        $endTime = $startTime + $duration;
    }
    

    Obviously, it's al simplified in this case. Because when I check for availability, and the spot is not free. I set the $startTime to be equal to the latest appointment found and start from there in the loop.

    Example:

    I check for a free spot at 9.00 but the spot is not free because there's an appointment from 9.00 till 10.00, then 10.00 is returned and $startTime is set to 10.00h instead of 9.30h. This is done to keep the number of queries to a minimum since there can be quiet a lot.
    

    Check availability function

    // Check Availability
    public static function checkAvailability($start, $end, $ape_id)
    {
      // add one second to the start to avoid results showing up on the full hour
      $start += 1;
      // remove one second from the end to avoid results showing up on the full hour
      $end -= 1;
    
      // $start and $end are timestamps
      $getAppointments = PRegistry::getObject('db')->query("SELECT * FROM appointments WHERE
        ((
            app_start BETWEEN '".date("Y-m-d H:i:s", $start)."' AND '".date("Y-m-d H:i:s", $end)."' 
              OR
            app_end BETWEEN '".date("Y-m-d H:i:s", $start)."' AND '".date("Y-m-d H:i:s", $end)."'
          ) 
        OR
          (
        app_start < '".date("Y-m-d H:i:s", $start)."' AND app_end > '".date("Y-m-d H:i:s", $end)."'
         ))
        AND
         ape_id = ".PRegistry::getObject('db')->escape($ape_id));
    
        if(PRegistry::getObject('db')->num_rows($getAppointments) == 0) {
          return true;
        } else {
          $end = 0;
          foreach(PRegistry::getObject('db')->fetch_array(MYSQLI_ASSOC, $getAppointments, false) as $app) {
            if($app['app_end'] > $end) {
              $end = $app['app_end'];
                }
        }
        return $end;
         } 
    }
    

    Since I'm storing appointments as "From:10.00 Till:11.00" I have to make sure to check spots from 11:00:01 till 11:59:59, because otherwise the appointment at 11:00 will show in the results.

    At the end of the function, in case an appointment is found, I loop the results and return the latest end. This is the next start in the loop I mentioned above.

    Hopefully this can be of any help to anyone. Just as info: ape_id is the ID of the "Appointment Person" it is linked with.

提交回复
热议问题