0

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>
Tarek
  • 3
  • 2
  • 1
    Please, [don't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). *They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation)*. See the [red box](http://uk.php.net/manual/en/function.mysql-connect.php)? Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://us1.php.net/pdo) or [MySQLi](http://us1.php.net/mysqli). [This article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which. – Jay Blanchard Sep 23 '14 at 17:46
  • im new in php and mysql, i know i've to use mysqli or PDO, but now i just want a help to solve this problem :) – Tarek Sep 23 '14 at 17:52

3 Answers3

0

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);
    ?>
Sujit Agarwal
  • 12,348
  • 11
  • 48
  • 79
  • Thnx for help ... but its the same .. the output is [{"dates":"2014-09-28"},{"dates":"2014-09-29"},{"dates":"2014-09-30"},{"dates":"2014-09-21"},{"dates":"2014-09-22"},{"dates":"2014-09-23"},{"dates":"2014-09-24"}] – Tarek Sep 23 '14 at 22:45
0

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);
Gervs
  • 1,397
  • 9
  • 8
  • it seems not working too .. Output is Fatal error: Allowed memory size of 67108864 bytes exhausted :( – Tarek Sep 23 '14 at 23:14
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;
sonal
  • 16
  • Thnx sonal .. it working now and the output is like: {"dates":["2014-09-18","2014-09-19","2014-09-20","2014-09-21","2014-09-22"]} What i tried to do now is to disable those dates in the datepicker but im also face a problem .. Just 1 day is disabled :( ill post my code for jquery i used and hope you can help me – Tarek Sep 24 '14 at 11:49