禁用datepicker中的所有日期,其中id = $ id [英] Disables all dates in datepicker where id =$id

查看:92
本文介绍了禁用datepicker中的所有日期,其中id = $ id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有这个表单:
agregareserva.php:

Hello I have first this form : agregareserva.php:

    <form action="GUImostrarcalendario.php" method="post" name="a">
    <select  type="text" id="id_habitacion"  name="id_habitacion" />
    <option value"1">ID 1</option>
    <option value="2">ID 2</option></select>
    <input type="submit" name="a"  value="Ver"/>
    </form>

在GUImostrarcalendario.php中我有:

In GUImostrarcalendario.php I have :

<?php
    include "controlreservas/conexion.php";
    $id_habitacion=$_POST["id_habitacion"];
    $sql1="SELECT llegada,salida,id_reserva FROM reservas 
    where id_habitacion ='$id_habitacion'";
    $query = $con->query($sql1);
    ?>
    <?php if($query->num_rows>0):?>
    <?php while ($r=$query->fetch_array()):?>
    <?php

    $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);
    $dates_ar = [];
    foreach ($daterange as $date) {
        $dates_ar[] = $date->format("Y-m-d");
    }
    $disabled_dates = '"' . implode('", "', $dates_ar) .'"';
    ?>
    <script>
    $(function() {
    var disabledDays = [<?php echo $disabled_dates; ?>];
    var date = new Date();
    jQuery(document).ready(function() { 
        $( "#datepicker9").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>

    <?php endwhile;?>
    <?php else:?>
    <?php endif;?>
    <input type="text" id="datepicker9" name ="datepicker9"/>

此代码用于在datepicker中显示预订中的所有日期(reservas),当id_habitacion(id room )在输入选择中选择。

This code is for show in the datepicker all dates in booking(reservas), when id_habitacion (id room) is select in the input select.

此代码正在工作,但仅在datepicker中显示数据库中的第一条记录,例如id_habitacion = 1的表:

This code is working but only show in the datepicker the first record in the database, for example the table where id_habitacion= 1 is :

我在预订中的所有日期这个id

i'ts all dates in booking for this id

我的代码生成datepicker:

And my code result in datepicker:

只显示一个预订日期

最后我需要datepicker显示所有日期,如何在表中显示

Finally I need the datepicker show all dates how in the table I show.

感谢。

推荐答案

您的代码目前正在创建多个实例

Your code currently creates multiple instances of

`$( "#datepicker9").datepicker({`

每个日期范围都有一个。您需要将代码更改为仅执行一次 - 所以在while循环之外

One for each date range. You need to change your code to only do this once - so, outside of the while loop

这需要$ date_ar在wile循环之外定义

This requires $date_ar be defined outside of the wile loop as well

此外,您不必要地使用连续的PHP代码片段

Also, you are using consecutive php fragments of code unnecessarily

代码应该是这样的

<?php
    include "controlreservas/conexion.php";
    $id_habitacion=$_POST["id_habitacion"];
    $sql1="SELECT llegada,salida,id_reserva FROM reservas where id_habitacion ='$id_habitacion'";
    $query = $con->query($sql1);
    $dates_ar = [];
    if($query->num_rows>0) {
        while ($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);
            foreach ($daterange as $date) {
                $dates_ar[] = $date->format("Y-m-d");
            }
        }
        ?>
        <script>
            $(function() {
                var disabledDays = <?php echo json_encode($date_arr)?>;
                var date = new Date();
                $( "#datepicker9").datepicker({ 
                    dateFormat: 'Y-m-d',
                    beforeShowDay: function(date) {
                        var m = date.getMonth() + 1, 
                            d = date.getDate(), 
                            y = date.getFullYear(),
                            strdate = [y,m,d].join('-');
                        if (disabledDays.indexOf(strdate) == -1) {
                            return [true, 'ui-state-active', ''];
                        }
                        return [false];
                    }
                });
            });
        </script>
        <?php
    }
?>
<input type="text" id="datepicker9" name ="datepicker9"/>

这包括@ charlietfl关于不使用implode的优点,但使用

This include @charlietfl's excellent point about not using implode, but using

var disabledDays = <?php echo json_encode($date_arr)?>;

替代

请注意,使用C编码风格

Note, I've used the "C" coding style

if() {
    while() {
    }
}

而不是您使用的备用样式

rather than the alternate style you used

if():
    while():
    endwhile;
else:
endif;

另外,没有 else 块,因为没有无论如何

Also, no else block since there's nothing to do anyway

这篇关于禁用datepicker中的所有日期,其中id = $ id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆