0

Hallo I'm trying to do a select query that should disable all the dates in my calendar based on the ones that are in the db. but for some reason it only want to do the last one entered in the db and I also can't disable the in between dates ( so for example between 4-7-2022 and 7-7-2022). I have been trying do use a for loop/ for each loop en eventually a while loop. but I'm not seeing where the problem is?

<?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 = "1";

 $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)){
    // do something
    //  foreach($data as $row):
       
        $start_datum = date('d-m-Y', strtotime($row['start_datum']));
       
        $eind_datum = date('d-m-Y', strtotime($row['eind_datum']));
        //echo $eind_datum , "<br>"; 
    
}
//  $data = $result->fetch_all(MYSQLI_ASSOC);
//  if ($data):
   ?> 


<script>

$(document).ready(function() {
      //make an array of disable dates
      //needs to be dates from db
      var dates = ["<?php echo $start_datum; ?>", "<?php echo $eind_datum; ?>"];
//var dates = ["10-07-2022", "15-07-2022", "30-07-2022", "11-07-2022", "16-07-2022", "31-07-2022"];

function disableDates(date) {
  var string = $.datepicker.formatDate('dd-mm-yy', date);
 
  return [dates.indexOf(string) <= -1];
  
}
console.log(dates);
    
    $( "#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 $start_datum; ?>", "<?php echo $eind_datum; ?>",];
    for(var i = 0; i < dates. length ; i++){
  
    console. log(dates[i])
    }
    function disableDates(date) {
        var string = $.datepicker.formatDate('dd-mm-yy', date);
        return [dates.indexOf(string) == -1];
    }
    //dit is voor als de rest werkt maar het zou de tussendagen weg moeten halen
    // if (dates <= <php $eind_datum ?> && dates => <php $start_datum ?>){
    //     console.log(testing);
    // }
    //var dates = ["10-07-2022", "15-07-2022", "30-07-2022","11-07-2022", "16-07-2022", "31-07-2022"];


    $( "#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>

database

  • You are in a while loop, so one assumes there are more than one Row in the resultset. You are over writing the variables `$start_datum` and `$eind_datum` in the loop, so after the loop you only see the last value for each of those – RiggsFolly Jul 04 '22 at 15:18
  • What is it you intend to do with these dates? – RiggsFolly Jul 04 '22 at 15:19
  • I want to use those dates to block the dates in my datepicker – user18149678 Jul 04 '22 at 15:20
  • because those dates are already booked – user18149678 Jul 04 '22 at 15:21
  • You have shown an example of the array you want `//var dates = ["10-07-2022", "15-07-2022", "30-07-2022", "11-07-2022", "16-07-2022", "31-07-2022"];` here, but those dates have nothing to do with the dates in your database. If that made more sense we might be able to work out what wants to be in that array – RiggsFolly Jul 04 '22 at 15:23
  • no that was een array with random dates to see if I could block certain dates in the calendar. – user18149678 Jul 04 '22 at 15:26
  • So not sure how to make a valid version of that array based on that – RiggsFolly Jul 04 '22 at 15:29
  • the array is suppossed to be based on the dates that are in the db. so I did var dates = ["", ""]; – user18149678 Jul 04 '22 at 15:32
  • How does whatever know which is a start date and which an end date. Think you need to do a little research – RiggsFolly Jul 04 '22 at 15:39
  • because I told them in the while loop. I said that $start_datum = date('d-m-Y', strtotime($row['start_datum'])); so that is start_datum? but I will have another look at it. – user18149678 Jul 04 '22 at 15:46
  • First! You are over writing the variables `$start_datum` and `$eind_datum` in the loop, so even if you have 1000 results from the query you will only ever get One start and One End date once you drop out of the while loop – RiggsFolly Jul 05 '22 at 09:05
  • Second! Once you have solved that issue, you load 2 php arrays into one javascript array, How will the javascript code know which of the lets say 10 start dates and 10 end dates is which in that array – RiggsFolly Jul 05 '22 at 09:06

0 Answers0