Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello is it possible to have a variable with mysqli results in them? and if so how?


<?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 * 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; ?>",];
    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>


What I have tried:

because I have tried to do 
<pre> var dates = [$row['start_date'], $row['end_date'], "30-07-2022"];


but this breaks my whole calendar. and I can't find any more ways to go about it. do any of you guys have an idea
Posted
Updated 4-Jul-22 4:45am
v2

1 solution

You almost have it correct, you can put PHP tags inside <script> elements! You've already formatted the dates into the $start_datum and $eind_datum variables so you just need to print them out at the correct spot:
var dates = ["<?php echo $start_datum; ?>", "<?php echo $eind_datum; ?>", "30-07-2022"];

Remember that PHP will pre-process the whole document so there's no limitation to where you can put the PHP tags. You may, however, want to consider moving the foreach() part though as it could potentially print out more than one </head><body> for the page which could break it. Try to keep any loops within certain boundaries:
<html>
<head>
  <!-- Fine to loop here -->
</head>
<body>
  <!-- Fine to loop here -->
</body>
</html>
 
Share this answer
 
Comments
minoesje minoes 4-Jul-22 8:49am    
thank you so much! but now I have another problem. say for example I have 2 start_datum and eind_datum in my db then it will still only block ( or disable) the first start_datum and end_datum it can find. how do I fix this?
minoesje minoes 4-Jul-22 10:32am    
I have tried to console log dates and it shows the two start_datum and the two eind_datum but for some reason it doesn't disable both of them.
Chris Copeland 4-Jul-22 10:47am    
I think foremost you need to re-examine the structure of the PHP page. Make sure to put scripts in the appropriate places and only loop where you absolutely need to. If your query is only ever returning one result however then for now the code you have should be fine.

As for other dates in the database not being excluded, well your query SELECT * FROM reserveringen WHERE huis_ID = ? only returns one result. If you want other start/end dates to be excluded you need to run a second select to select all start/end dates from the database, and then apply them to the script where you exclude dates.
minoesje minoes 4-Jul-22 10:57am    
sorry again but if my db has 2 of the same huis_ID then it should show both of them right? for example in my db: $huis_ID = 1; in my db there are 2 rows with huis_ID that are 1. so why isn't it showing both?
Chris Copeland 4-Jul-22 11:03am    
Okay I can see you've updated your code now. This is probably because you're looping over the multiple records however you're assigning the values to the same variables:

$start_datum = date('d-m-Y', strtotime($row['start_datum']));
$eind_datum = date('d-m-Y', strtotime($row['eind_datum']));


What's going to happen is the first result will be looped and assign the dates to these variables, and then the second result will loop and assign the new dates to the same variables, overwriting the results from the first! You probably instead need to store these dates in a PHP array, and then later in the script loop over the PHP array to print them in the Javascript part.

Alternatively, you could build a comma-delimited string and then just print out the string instead. I won't tell you how to do either of these approaches, you're probably better off doing some research online and working it out for yourself 😊

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