Diana Letelier
Diana Letelier

Reputation: 111

Disable days in a datepicker using select statement to database table

first code is for take dates between 2 dates from my table reservas...

<?php
include "controlreservas/conexion.php";
$sql1="select llegada, salida from reservas";
$query = $con->query($sql1);
$r=$query->fetch_array();
$begin = new DateTime( $r["llegada"] );
$end = new DateTime( $r["salida"] );
$end = $end->modify( '+1 day' ); 
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval ,$end);
?>
<?php foreach ($daterange as $date) : ?>
<?php echo $date->format("Y-m-d"); ?>

this it´s working :

2016-12-20
2016-12-21
2016-12-22
2016-12-23
2016-12-24

Now the next code is for disable this dates (all this dates, only get from the select ) in my datepicker :

<script>
$(function() {
var disabledDays = ["<?php echo $date->format("Y-m-d"); ?>"];
var date = new Date();
jQuery(document).ready(function() { 
    $( "#datepicker1").datepicker({ 
        dateFormat: 'Y-m-d',
        beforeShowDay: function(date) {
            var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
            for (i = 0; i < disabledDays.length; i++) {
                if($.inArray(y + '-' + (m+1) + '-' + d,disabledDays) != -1) {
                    //return [false];
                    return [true, 'ui-state-active', ''];
                }
            }
            return [true];

        }
    });
});
    });
</script>

the result is only disables first date, not all dates : only disable day 2016-12-20

thanks for you time

Upvotes: 1

Views: 528

Answers (1)

Dekel
Dekel

Reputation: 62676

The variable disabledDates (in javascript) should be an Array of strings, where each string represents a date that you want to disable.

What you can do is create that list from the $daterange variable that you have in php:

$dates_ar = [];
foreach ($daterange as $date) {
    $dates_ar[] = $date->format("Y-m-d");
}
$disabled_dates = '"' . implode('", "', $dates_ar) .'"';

And then in your javascript code you can use the $disabled_dates variable (which is a string that contains the values you need:

$(function() {
    var disabledDays = [<?php echo $disabled_dates; ?>];

Upvotes: 1

Related Questions