Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我想在“选择”下拉列表中动态禁用下两个值_Php_Wordpress - Fatal编程技术网

Php 我想在“选择”下拉列表中动态禁用下两个值

Php 我想在“选择”下拉列表中动态禁用下两个值,php,wordpress,Php,Wordpress,我正在制作活动日历,当一个用户预订了一个时间时,其他用户将无法选择该时间加上下两个时间段 就像我们有3:00 AM、3:30 AM、4:00 AM,直到24小时,如果用户选择3:00 AM,那么3:30 AM和4:00 AM也将被禁用 我已禁用凌晨3:00,但我没有找到一种方法来动态禁用凌晨3:30和凌晨4:00插槽 $date = '20190713'; $taken_slots = array(); $bookings_qry = $wpdb->get_results("SELE

我正在制作活动日历,当一个用户预订了一个时间时,其他用户将无法选择该时间加上下两个时间段 就像我们有3:00 AM、3:30 AM、4:00 AM,直到24小时,如果用户选择3:00 AM,那么3:30 AM和4:00 AM也将被禁用

我已禁用凌晨3:00,但我没有找到一种方法来动态禁用凌晨3:30和凌晨4:00插槽

$date = '20190713';

$taken_slots  = array(); 
$bookings_qry = $wpdb->get_results("SELECT booked_end_time FROM    wp_booked_time WHERE booked_date=$date");

foreach($bookings_qry as $bookings_row)
$taken_slots[] = $bookings_row->booked_end_time;

$slots_qry = $wpdb->get_results("SELECT * FROM wp_time");
$calendar  = '<select>'; 
$calendar .= '<option value="select" disabled="disabled">Select a Time</option>';

foreach($slots_qry as $slots_row){

$slot_id = $slots_row->time_id;

$calendar .= '<option value="'.$slot_id.'"';

 if(in_array($slot_id, $taken_slots)) 

   $calendar .= 'disabled="disabled"'; 


$calendar .= '>';    
$calendar.= $slots_row->times.'</option>';

}

$calendar .= '</select>';

echo $calendar;
$date='20190713';
$take_slots=array();
$bookings\u qry=$wpdb->获取结果(“从wp\u booked\u time中选择booked\u end\u time,其中booked\u date=$date”);
foreach($bookings\u qry作为$bookings\u行)
$taked\u slots[]=$bookings\u row->booked\u end\u time;
$slots\u qry=$wpdb->获取结果(“从wp\u时间选择*);
$calendar='';
$calendar.='选择一个时间';
foreach($slots\u qry作为$slots\u行){
$slot\u id=$slot\u row->time\u id;
$calendar.='';
$calendar.=$slots\u row->times.';
}
$calendar.='';
echo$日历;

在上面的代码中,我从wp_时间表中获取时间,并从数据库中匹配wp_booked_时间表中的预定值。现在请帮助我如何禁用下两个值。任何帮助都将不胜感激。提前感谢

操纵时间(以及迭代时间间隔)的最佳方法是使用本机PHP DateTime、DateInterval和DatePeriod。您需要检查当前单位间隔是否与现有约会重叠。下一步是检查所选的时段是否足够长,以满足所选/所需的单位数量。还有进一步定制的空间,但这应该(我希望)为您指明了正确的方向

<?php
/* Example "Office" or entity business rules (possibly loaded from DB?) */
$officeOpenTime = '08:00:00'; // 8am open
$officeCloseTime = '17:00:00'; // 5pm close
$officeMinPerUnit = 15; //Appt unit is 15 minutes long
$officeApptMinUnits = 2; //Appt minumum duration is 2 units (30 minutes)

/* Convert office minPerUnit to more useful DateInterval */
$unitInterval = new \DateInterval('PT'.$officeMinPerUnit.'M');

/* Day selected [presumably] from input */
$curSelectedDate = '2019-07-09';

/* Convert office open and close times to usable DateTime */
$curSelectedDayOfficeOpenDatetime = new \DateTime($curSelectedDate.' '.$officeOpenTime);
$curSelectedDayOfficeCloseDatetime = new \DateTime($curSelectedDate.' '.$officeCloseTime);

/* Iterable period per unit */
$curSelectedDayUnits = new \DatePeriod($curSelectedDayOfficeOpenDatetime, $unitInterval, $curSelectedDayOfficeCloseDatetime);

/* Current bookings test dataset- represents data from DB (make sure to ALWAYS order by datetime ascending)
 * Example (using open/close times):
 * SELECT * FROM appointments WHERE start_datetime < '2019-07-09 17:00:00' AND end_datetime > '2019-07-09 08:00:00' ORDER BY start_datetime ASC
 */
$testAppts = [
    [
        'start_datetime' => '2019-07-09 08:00:00',
        'end_datetime' => '2019-07-09 09:00:00'
    ],
    [
        'start_datetime' => '2019-07-09 11:00:00',
        'end_datetime' => '2019-07-09 11:30:00'
    ],
    [
        'start_datetime' => '2019-07-09 14:00:00',
        'end_datetime' => '2019-07-09 15:00:00'
    ],
    [
        'start_datetime' => '2019-07-09 15:00:00',
        'end_datetime' => '2019-07-09 16:00:00'
    ],
    [
        'start_datetime' => '2019-07-09 16:00:00',
        'end_datetime' => '2019-07-09 17:00:00'
    ]
];

?>
<html>
    <head>
    </head>
    <body>
        <form>
            <select>
                <option value="" disabled selected>Select a time</option>
<?php
$apptCurIndex = 0;
$numAppts = count($testAppts);

/* Convert first appt times to usable DateTime if exists */
if( $numAppts > 0 ){
    $curApptStartDatetime = new \DateTime($testAppts[$apptCurIndex]['start_datetime']);
    $curApptEndDatetime = new \DateTime($testAppts[$apptCurIndex]['end_datetime']);
}

foreach( $curSelectedDayUnits as $unitStartDatetime){
    echo '<option value="'.$unitStartDatetime->format('Y-m-d H:i:s').'"';

    /* Does slot need checking? */
    if( $numAppts > 0 ){
        /* Does slot need to be locked? */
        $unitEndDatetime = (clone $unitStartDatetime)->add($unitInterval);
        if( $unitEndDatetime > $curApptStartDatetime ){
            echo ' disabled';
        }

        /* Does current appt need to be incremented */
        if( $apptCurIndex < $numAppts - 1 && $unitEndDatetime >= $curApptEndDatetime ){
            $apptCurIndex++;
            $curApptStartDatetime = new \DateTime($testAppts[$apptCurIndex]['start_datetime']);
            $curApptEndDatetime = new \DateTime($testAppts[$apptCurIndex]['end_datetime']);
        }

    }

    echo '>'.$unitStartDatetime->format('g:i a').'</option>';
}

选择一个时间

操作时间(以及迭代时间间隔)的最佳方法是使用本机PHP DateTime、DateInterval和DatePeriod。您需要检查当前单位间隔是否与现有约会重叠。下一步是检查所选的时段是否足够长,以满足所选/所需的单位数量。还有进一步定制的空间,但这应该(我希望)为您指明了正确的方向

<?php
/* Example "Office" or entity business rules (possibly loaded from DB?) */
$officeOpenTime = '08:00:00'; // 8am open
$officeCloseTime = '17:00:00'; // 5pm close
$officeMinPerUnit = 15; //Appt unit is 15 minutes long
$officeApptMinUnits = 2; //Appt minumum duration is 2 units (30 minutes)

/* Convert office minPerUnit to more useful DateInterval */
$unitInterval = new \DateInterval('PT'.$officeMinPerUnit.'M');

/* Day selected [presumably] from input */
$curSelectedDate = '2019-07-09';

/* Convert office open and close times to usable DateTime */
$curSelectedDayOfficeOpenDatetime = new \DateTime($curSelectedDate.' '.$officeOpenTime);
$curSelectedDayOfficeCloseDatetime = new \DateTime($curSelectedDate.' '.$officeCloseTime);

/* Iterable period per unit */
$curSelectedDayUnits = new \DatePeriod($curSelectedDayOfficeOpenDatetime, $unitInterval, $curSelectedDayOfficeCloseDatetime);

/* Current bookings test dataset- represents data from DB (make sure to ALWAYS order by datetime ascending)
 * Example (using open/close times):
 * SELECT * FROM appointments WHERE start_datetime < '2019-07-09 17:00:00' AND end_datetime > '2019-07-09 08:00:00' ORDER BY start_datetime ASC
 */
$testAppts = [
    [
        'start_datetime' => '2019-07-09 08:00:00',
        'end_datetime' => '2019-07-09 09:00:00'
    ],
    [
        'start_datetime' => '2019-07-09 11:00:00',
        'end_datetime' => '2019-07-09 11:30:00'
    ],
    [
        'start_datetime' => '2019-07-09 14:00:00',
        'end_datetime' => '2019-07-09 15:00:00'
    ],
    [
        'start_datetime' => '2019-07-09 15:00:00',
        'end_datetime' => '2019-07-09 16:00:00'
    ],
    [
        'start_datetime' => '2019-07-09 16:00:00',
        'end_datetime' => '2019-07-09 17:00:00'
    ]
];

?>
<html>
    <head>
    </head>
    <body>
        <form>
            <select>
                <option value="" disabled selected>Select a time</option>
<?php
$apptCurIndex = 0;
$numAppts = count($testAppts);

/* Convert first appt times to usable DateTime if exists */
if( $numAppts > 0 ){
    $curApptStartDatetime = new \DateTime($testAppts[$apptCurIndex]['start_datetime']);
    $curApptEndDatetime = new \DateTime($testAppts[$apptCurIndex]['end_datetime']);
}

foreach( $curSelectedDayUnits as $unitStartDatetime){
    echo '<option value="'.$unitStartDatetime->format('Y-m-d H:i:s').'"';

    /* Does slot need checking? */
    if( $numAppts > 0 ){
        /* Does slot need to be locked? */
        $unitEndDatetime = (clone $unitStartDatetime)->add($unitInterval);
        if( $unitEndDatetime > $curApptStartDatetime ){
            echo ' disabled';
        }

        /* Does current appt need to be incremented */
        if( $apptCurIndex < $numAppts - 1 && $unitEndDatetime >= $curApptEndDatetime ){
            $apptCurIndex++;
            $curApptStartDatetime = new \DateTime($testAppts[$apptCurIndex]['start_datetime']);
            $curApptEndDatetime = new \DateTime($testAppts[$apptCurIndex]['end_datetime']);
        }

    }

    echo '>'.$unitStartDatetime->format('g:i a').'</option>';
}

选择一个时间

你能进一步解释一下吗。我不明白在我的情况下它将如何工作?@SiteSoft在我继续之前,请阅读代码注释。另外,请您复制粘贴代码,看看它是否真的能满足您的需要?请您进一步解释一下。我不明白在我的情况下它将如何工作?@SiteSoft在我继续之前,请阅读代码注释。您也可以复制粘贴代码,看看它是否真的能满足您的需要吗?