I have created a form that displays a list of database entries. There is a checkbox for each value. When I check the checkbox, I want to delete this entry.
My form class looks like this:
//Räume auslesen
$rooms = $DB->get_records_sql( 'SELECT LBTB.id, LBTB.datum, LBTB.roomid, LBTB.ustd, LBTB.userid, RAUM.raumname, RAUM.raumnummer FROM {bs_steinweg} LBTB
LEFT JOIN {bs_raume} RAUM
ON LBTB.roomid = RAUM.id
WHERE roomid > "0"
');
foreach ($rooms as $room) {
$newdate = date( 'd.m.Y', strtotime( $room->datum ) );
$mform->addElement('html', '<tr><th scope="row">');
$mform->addElement('advcheckbox', 'roomid[]','','','', $room->id);
$mform->addElement('html', '</th><td>');
$mform->addElement("html", "$room->raumname");
$mform->addElement('html', '</td><td>');
$mform->addElement("html", "$newdate");
$mform->addElement('html', '</td><td>');
$mform->addElement("html", "$room->ustd");
$mform->addElement('html', '</td></tr>');
$mform->addElement('hidden', 'id', $room->id);
$mform->setType('id', PARAM_INT);
}
I try to delete the data like this:
if ( !empty( $fromform->delete ) ) {
if ( !empty( $fromform->roomid ) ) {
foreach ( $fromform->roomid as $room_id ) {
print_r($fromform->id);
/* $DB->delete_records( 'bs_steinweg', [ 'id' => $room_id ] ); */
}
/* $getDeleteCanceld = get_string( 'getDeleteCanceld', 'local_buchungssystem' );
redirect( $CFG->wwwroot . '/local/buchungssystem/meine_fach_buchungen_sw.php', $getDeleteCanceld ); */
} else {
// Handle the case where no rooms were selected.
$getDeleteCanceld = get_string( 'noRoomsSelected', 'local_buchungssystem' );
redirect( $CFG->wwwroot . '/local/buchungssystem/meine_fach_buchungen_sw.php', $getDeleteCanceld );
}
}
The output with print_r($room_id)
does not give me any value.
The output with print_r($fromform->id)
only gives me the last value. Even if I select the first value, it always gives me the last ID.
Counting the whole thing up with a for loop also doesn’t output any value. It doesn’t matter whether I output print_r($room_id)
or print_r($fromform->id)
:
if ( !empty( $fromform->delete ) ) {
if ( !empty( $fromform->roomid ) ) {
foreach ( $fromform->roomid as $room_id ) {
for ( $i = 0; $i <= $room_id; $i++ ) {
print_r($room_id);
}
/* $DB->delete_records( 'bs_steinweg', [ 'id' => $room_id ] ); */
}
/* $getDeleteCanceld = get_string( 'getDeleteCanceld', 'local_buchungssystem' );
redirect( $CFG->wwwroot . '/local/buchungssystem/meine_fach_buchungen_sw.php', $getDeleteCanceld ); */
} else {
// Handle the case where no rooms were selected.
$getDeleteCanceld = get_string( 'noRoomsSelected', 'local_buchungssystem' );
redirect( $CFG->wwwroot . '/local/buchungssystem/meine_fach_buchungen_sw.php', $getDeleteCanceld );
}
}