Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to explain myself to google but with my limited english it isn't really getting me far.

I have this calendar that has disabled dates in them based on the dates that are in the db.
these dates have been put in an array.
let's say the array has the dates = 4/7/2022 and 7/7/2022
4/7/2022 is start_date
7/7/2022 is end_date
Right now it only disables those two dates. but what I want is some kind of loop that loops adds 1 disabled day from start_date until it reaches end_date

FULL CODE
<pre><?php include "config.php"; ?>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Display multiple months</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
  <script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>
  
<?php
//https://www.syncfusion.com/forums/152695/i-want-to-disable-the-datepicker-holiday-dates-which-are-stored-in-database
//https://www.google.com/search?q=javascript+make+connection+to+db&rlz=1C1CHZN_nlNL992NL992&oq=javascript+make+connection+to+db&aqs=chrome..69i57j33i160l3j33i22i29i30l2.7107j0j15&sourceid=chrome&ie=UTF-8
 $huis_ID = $_GET['ID'];
 $arraysession=array();
 $sql = "SELECT start_datum, eind_datum FROM reserveringen WHERE huis_ID = ?";
 $stmt = $conn->prepare($sql);
 $stmt->bind_param("i", $huis_ID);
 $stmt->execute();
 $result = $stmt->get_result();
 while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
       
         $start_datum = date('d-m-Y', strtotime($row['start_datum']));
       
         $eind_datum = date('d-m-Y', strtotime($row['eind_datum']));
        array_push($arraysession, $start_datum, $eind_datum);
    
 }

   $array_to_json=json_encode((array)$arraysession);
   ?> 


<script>

$(document).ready(function() {
      //make an array of disable dates
      //needs to be dates from db
      var dates = <?php echo $array_to_json?>;

function disableDates(date) {
  var string = $.datepicker.formatDate('dd-mm-yy', date);
 
  return [dates.indexOf(string) <= -1];
  
}
    
    $( "#datepicker" ).datepicker({
        dateFormat : "dd/mm/yy",
        todayHighlight:'TRUE',
        numberOfMonths: 1,
        minDate:0,
        showButtonPanel: true,
        beforeShowDay: disableDates
    

   
    });
});
$(document).ready(function() {
    //needs to be dates from db
    var dates = <?php echo $array_to_json ?>;
    console.log(dates);
    function disableDates(date) {
        var string = $.datepicker.formatDate('dd-mm-yy', date);
        for(var i = dates; i < $array_to_json. length ; i++){
        
        console. log(i);
        
        }
    
        return [dates.indexOf(string) == -1];
    }

    $( "#datepicker1" ).datepicker({
        dateFormat : "dd/mm/yy",
    numberOfMonths: 1,
    minDate:1,
    showButtonPanel: true,
    beforeShowDay: disableDates,
    
   
    });
});
</script>

<!-- 

        No data found
     -->
</head>
<body>
    <div class="check in mb-3">
        <label for="start_date" class="form-label">Start datum: </label>  
        <input type="text" class="form-control" id="datepicker" name="datepicker"/> 
    </div>

    <div class="check in mb-3">
        <label for="start_date" class="form-label">Eind datum: </label>  
        <input type="text" class="form-control" id="datepicker1" name="datepicker1"/> 
    </div>



</body>
</html>


What I have tried:

var dates = <?php echo $array_to_json?>;
function disableDates(date) {
var string = $.datepicker.formatDate('dd-mm-yy', date);
for(var i = dates; i < dates. length ; i++){
        
        console. log(string[i]);
        
        }
 return [dates.indexOf(string) == -1];
}

but this doesn't change anything and just shows the start_date and the end_date
then I tried
var dates = <?php echo $array_to_json?>;

function disableDates(date) {
        var string = $.datepicker.formatDate('dd-mm-yy', date);
        for(var i = string; i < string. length ; i++){
        
        console. log(string[i]);
        
        }
        return [dates.indexOf(string) == -1];
    }

and this shows the same thing.

tried switch up the positions of array and sting but that doesn't work either. what error am i making
Posted
Updated 4-Jul-22 17:31pm
v3
Comments
Richard MacCutchan 4-Jul-22 15:14pm    
This is the same issue as a number of your previous questions. You need a loop that starts at the first date, and adds 1 day until it reaches the end date. That is the simplest way to create an array, or to display a number of dates.
minoesje minoes 4-Jul-22 23:14pm    
I have already created an array and then put it into a jqeury variable but when I try to use that array again in my for loop it says it doesn't exist

 $huis_ID = $_GET['ID']; $arraysession=array(); $sql = "SELECT start_datum, eind_datum FROM reserveringen WHERE huis_ID = ?"; $stmt = $conn->prepare($sql); $stmt->bind_param("i", $huis_ID); $stmt->execute(); $result = $stmt->get_result(); while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){                $start_datum = date('d-m-Y', strtotime($row['start_datum']));                $eind_datum = date('d-m-Y', strtotime($row['eind_datum']));        array_push($arraysession, $start_datum, $eind_datum);     }   $array_to_json=json_encode((array)$arraysession);   ?> 


jquery
var dates = <?php echo $array_to_json ?>;
//$array_to_json work here 
    console.log(dates);
    function disableDates(date) {
        var string = $.datepicker.formatDate('dd-mm-yy', date);
        for(var i = dates; i < <?php $array_to_json ?>. length ; i++){
        //but doesn't work here in the loop
        console. log(i);
        
        }
    
        return [dates.indexOf(string) == -1];
    }
Richard MacCutchan 5-Jul-22 3:59am    
Yes, but your array contains only two dates, the start value and the end value. I cannot understand what is so difficult with this, as I gave you a suggested solution in your question at Calendar help getting the in between dates[^].
minoesje minoes 5-Jul-22 5:05am    
I have seen that one and from what I understand is I need to use the var dates because that is the one that changes and thats why I do: for(var i = dates; i < . length ; i++){ and even tried to do: for(var i = dates; i < dates.length; i++){ or for(var i = dates[0]; i < dates[1].length; i++){ because dates is the one that changes and date has the start avlue and the end value. this is what I understand from the whole story. so I don't understand why it wouldn't work as I even state that the code needs to grab the first one and the last one then i++ until it reaches the end.
Richard MacCutchan 5-Jul-22 5:24am    
Look at your loop above: what is the value of 'dates' at the start, and what gets output through the loop? It is no good saying, "but doesn't work", as we have no idea what that means. You need to show the values that are the input to the loop, and what gets printed as a result.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900