[英]Disable Dates within given ranges in jquery datepickers from PHP and Mysql
这是我的第一篇文章,请原谅任何错误
我是JQUERY和AJAX的新手,所以请您明确回答
我做了很多搜索,但无济于事,我正在用PHP MYSQLI和JQUERY构建公寓预订系统,我在同一页面上有2个jquery datepicker,一个除入住日期外,另一个除结帐日期外,MYSQL所使用的表格称为“预订”,其中包含以下各列-
ID,名字,电子邮件,签入日期,签出日期
可以说Mysql表返回2行,其中第一个返回check_in_date(2017-05-02)和check_out_date(2017-05-10)
以及第二个返回的check_in_date(2017-06-05)和check_out_date(2017-06-20)
如何禁用两个日期选择器在这2个范围内的所有日期,这就是我提出的
这是HTML表单的一部分
<div class="form-group">
<label for="check_in_date">Check In Date</label>
<input type="text" class="form-control" id="datepicker" placeholder="Check in Date">
</div>
<div class="form-group">
<label for="check_out_date">Check Out Date</label>
<input type="text" class="form-control" id="datepicker1" placeholder="Check Out Date">
</div>
这是Javascript-不完整
$(function() {
function checkAvailability(){
}
$( "#datepicker" ).datepicker({
dateFormat : 'yy-mm-dd',
beforeShowDay: checkAvailability
}
});
$( "#datepicker1" ).datepicker({
dateFormat : 'yy-mm-dd',
beforeShowDay: checkAvailability
}
});
});
PHP文件Availability.php如下
$result = mysqli_query("SELECT check_in_date, check_out_date FROM bookings");
while ($row = mysqli_fetch_assoc($result)){
$check_in_dates[] = $row['check_in_date'];
$check_out_dates[] = $row['check_out_date'];
}
提前致谢
我想通了这里是解决方案
<?php
//below function returns all the dates within a given range
function date_range($first, $last, $step = '+1 day', $output_format = 'Y-m-d' ) {//if it was simple y example y-m-d then it will show 17 instead of 2017
$dates = array();
$current = strtotime($first);
$last = strtotime($last);
while( $current <= $last ) {
$dates[] = date($output_format, $current);
$current = strtotime($step, $current);
}//end of while loop
return $dates;//returns a array
}//end of function
//select all the date ranges that exist in the database
$query = 'SELECT check_in, check_out FROM bookings';
$result = mysqli_query($db, $query);
$i = 0;
while ($row=mysqli_fetch_assoc($result)){
//output all the dates within the ranges in the database
$range[$i] = date_range($row['check_in'], $row['check_out']);//creates a associative array with numerical index values
$i++;
}
$individual_dates = array();
//converts the associative array into a regular array
foreach($range as $ranges){
foreach ($ranges as $many_ranges){
$individual_dates[] = $many_ranges;
}
}
$json_array = json_encode($individual_dates);
?>
这是javascript
<script>
var dateToday = new Date(); //creating new date obj. used to disable dates before today used in minDate: dateToday
$(function() {
$( "#datepicker" ).datepicker({
minDate: dateToday,
dateFormat : 'yy-mm-dd',
beforeShowDay: checkAvailability
});
$( "#datepicker1" ).datepicker({
minDate: dateToday,
dateFormat : 'yy-mm-dd',
beforeShowDay: checkAvailability
});
})
/******************THE json array returned by php is used here*****/
var $disabledDates = <?php echo $json_array; ?>
function checkAvailability(mydate){
var $return=true;
var $returnclass ="available";
$checkdate = $.datepicker.formatDate('yy-mm-dd', mydate);
for(var i = 0; i < $disabledDates.length; i++)
{
if($disabledDates[i] == $checkdate)
{
$return = false;
$returnclass= "unavailable";
}
}
return [$return,$returnclass];
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.