从一个数组中的mysql中检索多个结果

时间:2014-09-23 17:44:03

标签: php mysql arrays datepicker

我面临的问题是我希望将从mysql检索到的结果转换为一个数组: {"日期":" 2014年9月28日"" 2014年9月29日"" 2014年9月30日"} 尽管如此 {"日期":" 2014年9月28日"} {"日期":" 2014年9月29日"} {&#34 ;日期":" 2014年9月30日"} 它将用于在我的日期选择器中禁用另一页面上的那些日期 这是我的源代码

    <?php
@session_start;
include '../includes/db_login.php';
$flat_id= $_GET['flat_id']; 

 $result = mysql_query("SELECT startDate,endDate FROM reservation WHERE flat_id='$flat_id'");
 $i=0;
 while($row = mysql_fetch_array($result))
    {


$s_dte=$row['startDate'];
$s_dte2=strtotime($s_dte);
$s=date("Y-m-d",$s_dte2);
$e_dte=$row['endDate'];
$diff = abs(strtotime($e_dte) - strtotime("-1 day",strtotime($s))); 



$yrs   = floor($diff / (365*60*60*24)); 
$mnth  = floor(($diff - $yrs * 365*60*60*24) / (30*60*60*24)); 
$days    = floor(($diff - $yrs * 365*60*60*24 - $mnth*30*60*60*24)/ (60*60*24));
$t=1;


while($t <= $days){

//echo $s."\n";
$confirmedSends = array ( "dates" => $s);
$date = strtotime("+1 day", strtotime($s));
$s=date("Y-m-d", $date);
$t++;
$jsonConfirmedSends =  json_encode($confirmedSends); 
    echo $jsonConfirmedSends;
}

}

?>

我现在得到了所需的输出,但我仍然无法在jquery上禁用日期,这是我用于jquery的源代码

jQuery(document).ready(function () {

                    $("#datepick").datepicker({
                        defaultDate: "d",
                        dateFormat: 'yy-mm-dd',
                        changeMonth: true,
                        numberOfMonths: 1,
                        beforeShowDay: checkAvailabilityStart,
                        onClose: function (selectedDate) {
                            $("#datepick2").datepicker("option", "minDate", selectedDate);
                        }
                    });



                    var dates = [];
                    var flat_id = $("#flat_id").val();



                    $.getJSON("ajax.php?flat_id=" + flat_id, function (data) {
         $.each(data, function(index, value) {
            dates.push(value.data); // i don't know what's (value.data) refer to !
        });
    });


                    function checkAvailabilityStart(mydate) {
                        var $return = true;
                        var $returnclass = "available";
                        $checkdate = $.datepicker.formatDate('yy-mm-dd', mydate);
                        for (var i = 0; i < dates.length; i++)
                        {
             if (dates[i] == $checkdate)              

                            {
                                $return = false;
                                $returnclass = "unavailable";
                            }
                        }
                        return [$return, $returnclass];
                    }



                });


            </script>

3 个答案:

答案 0 :(得分:0)

您需要多维数组。检查代码中的更改 -

    <?php
    @session_start;
    include '../includes/db_login.php';
    $flat_id= $_GET['flat_id']; 

    $result = mysql_query("SELECT startDate,endDate FROM reservation WHERE flat_id='$flat_id'");
    $i=0;
    $final = array();
    while($row = mysql_fetch_array($result))
    {


        $s_dte=$row['startDate'];
        $s_dte2=strtotime($s_dte);
        $s=date("Y-m-d",$s_dte2);
        $e_dte=$row['endDate'];
        $diff = abs(strtotime($e_dte) - strtotime("-1 day",strtotime($s))); 



        $yrs   = floor($diff / (365*60*60*24)); 
        $mnth  = floor(($diff - $yrs * 365*60*60*24) / (30*60*60*24)); 
        $days    = floor(($diff - $yrs * 365*60*60*24 - $mnth*30*60*60*24)/ (60*60*24));
        $t=1;


        while($t <= $days){

//echo $s."\n";
            $confirmedSends = array ( "dates" => $s);
            $date = strtotime("+1 day", strtotime($s));
            $s=date("Y-m-d", $date);
            $t++;
            $final[] = $confirmedSends;
            //$jsonConfirmedSends =  json_encode($confirmedSends); 
            //echo $jsonConfirmedSends;
        }
    }
    echo json_encode($final);
    ?>

答案 1 :(得分:0)

稍短一点

$dates = array('dates' =>array());
while ($row = mysqli_fetch_assoc($result)) {
    $date = strtotime($row['startDate']);
    $enddate = strtotime($row['endDate']);
    while ($date <= $enddate) {
        $dates['dates'][] = date('Y-m-d', $date);
        $date += 86400;
    }

}
echo json_encode($dates);

答案 2 :(得分:0)

instead of below code,

while($t <= $days){

//echo $s."\n";
$confirmedSends = array ( "dates" => $s);
$date = strtotime("+1 day", strtotime($s));
$s=date("Y-m-d", $date);
$t++;
$jsonConfirmedSends =  json_encode($confirmedSends); 
    echo $jsonConfirmedSends;
}

Try below code :

while($t <= $days){

//echo $s."\n";
$confirmedSends["dates"][] = $s;
$date = strtotime("+1 day", strtotime($s));
$s=date("Y-m-d", $date);
$t++;
}

$jsonConfirmedSends =  json_encode($confirmedSends); 
echo $jsonConfirmedSends;