0

I have table from database here, where I want to fetch the times only (fld_booking_pickup, fld_booking_start, fld_booking_end in tbl_bookings database here), in order to disable particular time here that have been selected in database via timepicker.

This is the coding that I set for the times :

<div class="form-group">
 <label for="Pickup">Pickup Time</label>
 <input type="time" class="form-control timepicker" name="fld_booking_pickuptime" id="fld_booking_pickuptime" placeholder="hh:mm A" value="" required>
</div>
<div class="form-group">
 <label for="StartTime">Program Time (Start)</label>
 <input type="time" class="form-control timepicker" name="fld_booking_start" id="fld_booking_start" placeholder="hh:mm A" value="" required>
</div>
<div class="form-group">
 <label for="EndTime">Program Time (End)</label>
 <input type="time" class="form-control timepicker" name="fld_booking_end" id="fld_booking_end" placeholder="hh:mm A" value="" required>
</div>

This is the script that I use to disable time selection (I thought of replacing date with time in script, but I could be wrong since I really don't know any other ways for time, since I'm totally new in this) :

<?php
$times = array();
        
$time_query = "SELECT `tbl_bookings`.`fld_booking_pickuptime`, `tbl_bookings`.`fld_booking_start`, `tbl_bookings`.`fld_booking_end` FROM `tbl_bookings` WHERE status_delete=1";
$time_result = mysqli_query($link, $time_query);
  if (mysqli_num_rows($time_result ) > 0) {
    while ($row = mysqli_fetch_assoc($time_result )) {
       $times [] = $row['fld_booking_pickuptime'], $row['fld_booking_pickuptime'], $row['fld_booking_pickuptime'];
    }
  }
?>

  var array = <?= json_encode($times)?>;

$('input.timepicker').timepicker({
    beforeShowDay: function(time){
        var string = jQuery.timepicker.formatTime('hh:mm A', date);
        return [ array.indexOf(string) == -1 ]
    }
});

I want to fetch the respective times from database as the array, so that the times can be disabled for selection (using times that already in database). I am totally new in this, so I hope anyone can somewhat help me to fetch the times from database for the time selection disable. Thank you so much, totally appreciated ~

Xun
  • 377
  • 1
  • 7
NAFIS
  • 1
  • 1
  • Your `for` attributes are not pointing to elements with a matching `id` value, so they will not work properly. Just so you know, you can simply write `foreach (mysqli_query($link, $time_query) as $row) {`. And `$times [] = $row['fld_booking_pickuptime'], $row['fld_booking_pickuptime'], $row['fld_booking_pickuptime'];` looks like invalid PHP to me. What is your intention? – mickmackusa Jan 11 '23 at 04:22
  • @mickmackusa I already fix the invalid PHP part as the answer suggested, & may I know about the for attributes for the row? My intention is to fetch those 3 data so that for new booking upon the same date, the times in them will be disabled to be selected. – NAFIS Jan 11 '23 at 04:44
  • https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label You don't need to write `$times` three times like the answer says. Just wrap the comma-separated values in square braces. `$times[] = [$one, $two, $three];` – mickmackusa Jan 11 '23 at 05:03
  • @mickmackusa I think I did like that `$times [] = $row['fld_booking_pickuptime'], $row['fld_booking_pickuptime'], $row['fld_booking_pickuptime'];`, or did I do wrong? – NAFIS Jan 11 '23 at 07:41
  • `$times[] = [$row['fld_booking_pickuptime'], $row['fld_booking_pickuptime'], $row['fld_booking_pickuptime']];` notice the extra square braces. – mickmackusa Jan 11 '23 at 11:30
  • @mickmackusa alright got it, now I want to know about `foreach` that you said there, since I still couldn't get it + where I shall put the code if needed? – NAFIS Jan 12 '23 at 02:42
  • I only meant to inform you that [you can put your query call inside of a foreach call](https://stackoverflow.com/a/72440754/2943403) to reduce the overall code size. Beyond that, I don't understand your question, so I don't intend to post an answer. – mickmackusa Jan 12 '23 at 03:14
  • @mickmackusa ah I see it's for code size, I thought it's for any more functions to fetch the data, since the times still couldn't be disabled based on the database from both times data here & date that are already filled – NAFIS Jan 12 '23 at 03:44

0 Answers0