В настоящее время я программирую систему бронирования для ноутбука. В этой системе вы можете указать дату начала и дату окончания. Я использую эти две даты для прохождения DatePeriod в качестве foreach. Ноутбуки можно забронировать на разное время. Поэтому у меня есть цикл for в foreach, который считает столько раз, сколько указано в часах. Для выбора часов доступны два поля выбора. После отправки формы я проверяю, достаточно ли еще ноутбуков. Есть максимальное количество ноутбуков, которое я засчитал.
//Create DatePeriod. $fromform->date is the start date and $fromform->reservto is the end date.
$startDateTime = new DateTime();
$endDateTime = new DateTime();
$newEndDateTime = strtotime( '+1 day', $fromform->reservto );
$dateInterval = new DateInterval( 'P7D' );
$datePeriod = new DatePeriod( $startDateTime->setTimestamp( $fromform->date ), $dateInterval, $endDateTime->setTimestamp( $newEndDateTime ) );
foreach ( $datePeriod as $dateTime ) {
//Count up the hours. $fromform->time is the first hour selected. $fromform->timeto is the last hour selected.
for ( $i = $fromform->time; $i timeto; $i++ ) {
$comparedate = $DB->sql_compare_text( 'datum' );
$comparedateplaceholder = $DB->sql_compare_text( ':date' );
$compareustd = $DB->sql_compare_text( 'ustd' );
$compareustdplaceholder = $DB->sql_compare_text( ':ustd' );
$newdate = $dateTime->format( 'Y-m-d' );
//Read the number of reserved laptops.
$reservlapptopsreserv = $DB->get_records_sql(
"SELECT SUM(anzahl) AS sum_laptops FROM {bs_steinweg} WHERE
{$comparedate} = {$comparedateplaceholder}
AND
{$compareustd} = {$compareustdplaceholder}
GROUP BY
anzahl
",
[
'date' => $newdate,
'ustd' => $fromform->time,
]
);
//Calculate the total number of laptops.
$reservelapreserv = 0;
foreach ( $reservlapptopsreserv as $resLapreserv ) {
$reservelapreserv += $resLapreserv->sum_laptops;
}
//Read maximum number of laptops
$anzreserv = $DB->get_record( 'bs_laptops', array( 'id'=>$fromform->laptop ), 'anzahl' );
//Calculate available laptops.
//Maximum laptop number - Number of reserved laptops - Specified new laptop - Number to be booked (data from the form)
$rechnlapanzreserv = $anzreserv->anzahl - $reservelapreserv - $fromform->timelaptopanzahl;
//If the calculation results in >= 0, then create the database entry.
if ( $rechnlapanzreserv >= 0 ) {
$newdate = $dateTime->format( 'Y-m-d' );
$recordtoinsert = new stdClass();
$recordtoinsert->datum = $newdate;
$recordtoinsert->ustd = $i;
$recordtoinsert->resfuer = $fromform->resfuer;
$recordtoinsert->roomid = '0';
$recordtoinsert->laptopid = $fromform->laptop;
$recordtoinsert->anzahl = $fromform->timelaptopanzahl;
$bgcolor = 'green';
$recordtoinsert->userid = $fromform->userid;
$recordtoinsert->bgcolor = $bgcolor;
$DB->insert_record( 'bs_steinweg', $recordtoinsert );
//If the calculation results in < 0, then create an info database entry to show the user that the booking did not work.
} elseif ( $rechnlapanzreserv < 0 ) {
//Laptops auslesen
$compareid = $DB->sql_compare_text( 'id' );
$compareidplaceholder = $DB->sql_compare_text( ':id' );
$laptop = $DB->get_record_sql(
"SELECT * FROM {bs_laptops} WHERE {$compareid} = {$compareidplaceholder}",
[
'id' => $fromform->laptop,
]
);
$newdate = $dateTime->format( 'Y-m-d' );
$recordtoinsert = new stdClass();
$recordtoinsert->userid = $fromform->userid;
$recordtoinsert->datum = $newdate;
$recordtoinsert->ustd = $i;
$recordtoinsert->laptop = $laptop->laptop . " (" . $laptop->raum . ") " . " (" . $rechnlapanzreserv . ") " ;
$recordtoinsert->raum = '0';
$recordtoinsert->standort = 'Steinweg';
$DB->insert_record( 'bs_nobooking', $recordtoinsert );
}
}
}
$getsaved = get_string( 'saved', 'local_buchungssystem' );
redirect( $CFG->wwwroot . '/local/buchungssystem/bs_sw.php', $getsaved );
Теперь у меня проблема: в течение первого часа создается запись в базе данных, но в течение остальных часов я получаю сообщение об ошибке. Результат вычисления всегда равен
В настоящее время я программирую систему бронирования для ноутбука. В этой системе вы можете указать дату начала и дату окончания. Я использую эти две даты для прохождения DatePeriod в качестве foreach. Ноутбуки можно забронировать на разное время. Поэтому у меня есть цикл for в foreach, который считает столько раз, сколько указано в часах. Для выбора часов доступны два поля выбора. После отправки формы я проверяю, достаточно ли еще ноутбуков. Есть максимальное количество ноутбуков, которое я засчитал. [code]//Create DatePeriod. $fromform->date is the start date and $fromform->reservto is the end date. $startDateTime = new DateTime(); $endDateTime = new DateTime(); $newEndDateTime = strtotime( '+1 day', $fromform->reservto ); $dateInterval = new DateInterval( 'P7D' ); $datePeriod = new DatePeriod( $startDateTime->setTimestamp( $fromform->date ), $dateInterval, $endDateTime->setTimestamp( $newEndDateTime ) );
foreach ( $datePeriod as $dateTime ) { //Count up the hours. $fromform->time is the first hour selected. $fromform->timeto is the last hour selected. for ( $i = $fromform->time; $i timeto; $i++ ) { $comparedate = $DB->sql_compare_text( 'datum' ); $comparedateplaceholder = $DB->sql_compare_text( ':date' ); $compareustd = $DB->sql_compare_text( 'ustd' ); $compareustdplaceholder = $DB->sql_compare_text( ':ustd' ); $newdate = $dateTime->format( 'Y-m-d' ); //Read the number of reserved laptops. $reservlapptopsreserv = $DB->get_records_sql( "SELECT SUM(anzahl) AS sum_laptops FROM {bs_steinweg} WHERE {$comparedate} = {$comparedateplaceholder} AND {$compareustd} = {$compareustdplaceholder} GROUP BY anzahl ", [ 'date' => $newdate, 'ustd' => $fromform->time, ] );
//Calculate the total number of laptops. $reservelapreserv = 0; foreach ( $reservlapptopsreserv as $resLapreserv ) { $reservelapreserv += $resLapreserv->sum_laptops; }
//Read maximum number of laptops $anzreserv = $DB->get_record( 'bs_laptops', array( 'id'=>$fromform->laptop ), 'anzahl' );
//Calculate available laptops. //Maximum laptop number - Number of reserved laptops - Specified new laptop - Number to be booked (data from the form) $rechnlapanzreserv = $anzreserv->anzahl - $reservelapreserv - $fromform->timelaptopanzahl;
//If the calculation results in >= 0, then create the database entry. if ( $rechnlapanzreserv >= 0 ) { $newdate = $dateTime->format( 'Y-m-d' ); $recordtoinsert = new stdClass(); $recordtoinsert->datum = $newdate; $recordtoinsert->ustd = $i; $recordtoinsert->resfuer = $fromform->resfuer; $recordtoinsert->roomid = '0'; $recordtoinsert->laptopid = $fromform->laptop; $recordtoinsert->anzahl = $fromform->timelaptopanzahl; $bgcolor = 'green'; $recordtoinsert->userid = $fromform->userid; $recordtoinsert->bgcolor = $bgcolor;
//If the calculation results in < 0, then create an info database entry to show the user that the booking did not work. } elseif ( $rechnlapanzreserv < 0 ) {
} } $getsaved = get_string( 'saved', 'local_buchungssystem' ); redirect( $CFG->wwwroot . '/local/buchungssystem/bs_sw.php', $getsaved ); [/code] Теперь у меня проблема: в течение первого часа создается запись в базе данных, но в течение остальных часов я получаю сообщение об ошибке. Результат вычисления всегда равен