Disable days in a datepicker if records already exist

4

I'm making a system where patients can request appointments online.

At the moment I have a datepicker in my code as follows

<script>
                $( function() {
                    $.datepicker.setDefaults($.datepicker.regional["es"]);
                    $( "#datepicker" ).datepicker({
                        dateFormat: 'dd-mm-yy',
                        changeMonth:true,
                        changeYear:true,
                        yearRange: "1990:2017", 
                        firstDay: 1,
                        minDate: 0,
                         beforeShowDay: function (day) { 
                            var day = day.getDay(); 
                            if (day == 6 || day == 0) { 
                            return [false, "somecssclass"] 
                             } else { 
                                    return [true, "someothercssclass"] 
                                    } 
                            }, 

                        monthNames: ['Enero', 'Febrero', 'Marzo',
                        'Abril', 'Mayo', 'Junio',
                        'Julio', 'Agosto', 'Septiembre',
                        'Octubre', 'Noviembre', 'Diciembre'],
                        monthNamesShort: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun',
                        'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'],
                        dayNamesMin: ['Dom', 'Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab'],

                        closeText: 'Cerrar',
                        prevText: 'Anterior',
                        nextText: 'Siguiente',
                        currentText: 'Hoy',
                    });
                });



     </script>

In this case I could disable the days before the current one and on Saturdays and Sundays since, because they are online, the aforementioned ones should not be selectable.

Now I just have to disable those days that already have the maximum number of appointments. In the office only 20 appointments are attended daily, I would like if for example today there are already 20 registered appointments, and today is disabled in the datepicker, how could I achieve that? ... I have not yet done the code to limit daily appointments, I would like to also give me a help with that if they know how to take advantage of the question since I'm really new to this part of validations and the system I'm doing it because it's my university project and I've researched but only I get more information about blocking dates directly and not by registrations.

ADDITIONAL

I had thought to do a SELECT COUNT (*) to limit the appointments for days, but I do not know how to call in that case what would come after the select. In this case, the CITES table (idCitas, fechaCita, descripcionCita, foreign key of typeExamen) is the one that would contain the date of the appointment that is selected by datepicker.

My Database citations is structured as follows:

fcitas is the date of the appointment and exam_idExamen is the id of the exams that the office makes to the public. that exam table is structured like this

Attention time in this table is the time that belongs to that type of exam. It is fixed and only changed if the administrator wants it.

My select count I did it in the following way

$result = $mysqli->query("SELECT COUNT(*) FROM citas");
$row = @mysqli_fetch_row($result);
if ($row[0] > 20) {

    echo 'el limite de citas se ha superado';
}
else {



        $registro = registraCita($fCita, $horaAtencion, $examen_idExamen, $cedula); 



}

To begin with I thought so, but I think that in that case loo would be limiting the whole table to appointments and not to the days.

UPDATE

UPDATE 2 To limit appointments for days I am doing the following in my class citations.php

$result = $mysqli->query("SELECT COUNT(fcita) FROM citas WHERE fcita='$fCita'");
                    $row = @mysqli_fetch_row($result);
                    if ($row[0] > 20) {



                        $errors[] = "La fecha seleccionada ya ha superado el limite de citas";



                    }
                    else {



                            $registro = registraCita($fCita, $horaAtencion, $examen_idExamen, $cedula, $fechaSolicitud, $nombreExamen);




                    }

and in the datePicker (also located in citations.php ) which is where the person selects the date they want their appointment, I have the following (limiting that I can not choose days before the current and ending of the week)

<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" /> <!--date picker-->
    <script src="../login/assets/js/bootstrap.min.js" ></script>
    <script src="../login/assets/js/jquery-3.1.1.min.js"></script> 
    <script src="../login/assets/js/jquery-ui.js"></script> <!--date picker-->




     <script>

    function nDates(date){  
      var nDates = 0;
      $.ajax({
            type: "POST",
            url: 'prueba.php',
            data: (date),
            success: function(data) {
                nDates = data;
            },
            error: function() {
                alert('Error occured');
            }
        });
      return nDates
    }

                    $( function() {
                        $.datepicker.setDefaults($.datepicker.regional["es"]);
                        $( "#datepicker" ).datepicker({
                            dateFormat: 'yy-mm-dd',
                            changeMonth:true,
                            changeYear:true,
                            yearRange: "1990:2017", 
                            firstDay: 1,
                            minDate: 0,
                            beforeShowDay: function (date) {
                                var datesLimits = 20; 
                                var day = date.getDay(); 
                                var date = date.getDate() +"-"+date.getMonth() +"-"+date.getFullYear();
                                if (day == 6 || day == 0) { 
                                return [false, "somecssclass"] 
                                 } else { 
                                   if (nDates(date) < datesLimits){
                                        return [true, "someothercssclass"] 
                                   } else {                                   
                                        return [false, "somecssclass"]
                                      }
                                }},
                            monthNames: ['Enero', 'Febrero', 'Marzo',
                            'Abril', 'Mayo', 'Junio',
                            'Julio', 'Agosto', 'Septiembre',
                            'Octubre', 'Noviembre', 'Diciembre'],
                            monthNamesShort: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun',
                            'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'],
                            dayNamesMin: ['Dom', 'Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab'],

                            closeText: 'Cerrar',
                            prevText: 'Anterior',
                            nextText: 'Siguiente',
                            currentText: 'Hoy',
                        });
                    });



         </script>

Precisely in this part of the code

else { 
                                       if (nDates(date) < datesLimits){
                                            return [true, "someothercssclass"] 
                                       } else {                                   
                                            return [false, "somecssclass"]
                                          }

is where it should be working so that if the day already has 20 appointments, it should be disabled, but it is not, what I notice is that the condition is always working because of 'return true' after reading the condition nDates (date) < dateslimits, but probe placing 'false false' after that line, and what it does is disable it completely every day of the datepicker.

in prueba.php I have the following code

<?php 
    require '../conexion/conexion.php';


$result = $mysqli->query("SELECT COUNT(fcita) FROM citas WHERE fcita='$fcita'");
                        $respuesta = true;
                        $row = @mysqli_fetch_row($result);
                        if ($row[0] > 20) {
                            $respuesta = false;
                        }
                        else {
                            $respuesta = true;
                        }
echo $respuesta ;


?>

when entering localhost / system / citations / prueba.php what it loads is 1, that is, the return true does it correctly, now I thought about placing WHERE fcita = '2017/08/16' ", when reloading the page, the 1 automatically disappears and remains blank, that is, the false return is activated because already on that date I have 20 registered appointments.

For now I do not know if I'm missing any additional libraries that I showed before starting the datepicker script, or I'm doing something wrong even to disable the days that are already full.

    
asked by rodrigo2324 07.08.2017 в 07:04
source

1 answer

1
  

NEW RESPONSE EDITION

The problem that has been taking place is because of the AJAX request. When making several asynchronous requests the variable nDates (nDates) always took the value false . In this way every day they were either disabled or enabled.

DataPicker

$.datepicker.setDefaults($.datepicker.regional["es"]);
                    $( "#datepicker" ).datepicker({
                        dateFormat: 'dd-mm-yy',
                        changeMonth:true,
                        changeYear:true,
                        yearRange: "1990:2017", 
                        firstDay: 1,
                        minDate: 0,
                        beforeShowDay: function (date) {
                            var datesLimits = 20; 
                            var day = date.getDay(); 
                            var dd = date.getDate();
                            var mm = date.getMonth()+1; 
                            var yyyy = date.getFullYear();
                            if(dd<10){
                                dd='0'+dd;
                            } 
                            if(mm<10){
                                mm='0'+mm;
                            }
                            var date = yyyy+'-'+mm+'-'+dd;
                            if (day == 6 || day == 0) { 
                            return [false, "somecssclass"] 
                             } else {
                               if (nDates(date)){
                                    return [true, "someothercssclass"] 
                               } else {                                  
                                    return [false, "somecssclass"]
                                  }
                            }},
                        monthNames: ['Enero', 'Febrero', 'Marzo',
                        'Abril', 'Mayo', 'Junio',
                        'Julio', 'Agosto', 'Septiembre',
                        'Octubre', 'Noviembre', 'Diciembre'],
                        monthNamesShort: ['Ene', 'Feb', 'Mar', 'Abr', 'May', 'Jun',
                        'Jul', 'Ago', 'Sep', 'Oct', 'Nov', 'Dic'],
                        dayNamesMin: ['Dom', 'Lun', 'Mar', 'Mie', 'Jue', 'Vie', 'Sab'],
                        closeText: 'Cerrar',
                        prevText: 'Anterior',
                        nextText: 'Siguiente',
                        currentText: 'Hoy',
                    });
                });

AJAX PETITION

Adding async : false to the AJAX request will make one request after another waiting for the previous one to be completed first. This slows down the performance of the application noticeably .

  function nDates(date){  
              var nFechas = false;
              $.ajax({
                    type: "POST",
                    url: 'consulta.php',
                    data: {date},
                    async: false,
                    success: function(data) {
                      if (data == 1)
                        nFechas = true;
                      else 
                        nFechas = false;
                    },
                    error: function() {
                        alert('Error occured');
                    }
                });
              return nFechas;
            }

PHP CONSULTATION

As you well know this is not the best way to make a query in php, but to make a simple query should serve as an example.

<?php
include('conexion.php');

$fCita = $_POST['date'];

$result = $conn->query("SELECT COUNT(*) FROM citas WHERE fcita='$fCita'");
                    $respuesta = true;
                    $row = @mysqli_fetch_row($result);
                    if ($row[0] >= 20) {
                        $respuesta = 0;
                    }
                    else {
                        $respuesta = 1;
                    }
 echo $respuesta; 
 $conn->close();
?>
    
answered by 07.08.2017 / 16:03
source