禁用datepicker中id = $ id的所有日期

时间:2016-12-18 02:32:11

标签: javascript php jquery datepicker

你好,我有第一个这样的表格: agregareserva.php:

<?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"/>

在GUImostrarcalendario.php中我有:

        Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

            If e.Url.AbsolutePath <> TryCast(sender, WebBrowser).Url.AbsolutePath Then
                Return
            End If
            For Each elem As HtmlElement In WebBrowser1.Document.GetElementsByTagName("LI")
                If elem.InnerText = "Gange med 0" Then
                    elem.InvokeMember("click")
                End If

            Next
            For Each elem2 As HtmlElement In WebBrowser1.Document.GetElementsByTagName("BUTTON")
                If elem2.!!PROBLEM IS HERE!! = "go" Then
                    elem2.InvokeMember("click")
                End If
            Next



        End Sub

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

此代码正常运行,但只在datepicker中显示数据库中的第一条记录,例如id_habitacion = 1的表:

Elementwise Addition

我预订此ID的所有日期

我的代码导致了datepicker:

enter image description here

仅显示一个预订日期

最后我需要datepicker显示所有日期我在表格中的显示方式。

Thankss。

1 个答案:

答案 0 :(得分:1)

您的代码目前创建了多个

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

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

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

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

代码应如下所示

<?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关于不使用内爆,但使用

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

代替

注意,我已经使用了&#34; C&#34;编码风格

if() {
    while() {
    }
}

而不是您使用的替代风格

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

此外,没有else阻止,因为无论如何都无法做到