简体   繁体   中英

retrieve multiple results from mysql in one array

The problem i face is that i want to get the results retrieved from mysql into one array as: {"dates":"2014-09-28","2014-09-29","2014-09-30"} inspite of this result {"dates":"2014-09-28"}{"dates":"2014-09-29"}{"dates":"2014-09-30"} that it will use to disable those dates on my datepicker in another page that's my source code

    <?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;
}

}

?>

I got the required output now but i still can't disable dates on the jquery, that is my source code i used for 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>

Multidimensional array is what you need. Check the changes in code -

    <?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);
    ?>

A bit shorter

$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);
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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM