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 );
}
}
My solution:
$mform->addElement('advcheckbox', "laptopid[$laptop->id]",'','','', $laptop->id);
The processing:
if ( !empty( $fromform->delroomsw ) ) {
if ( !empty( $fromform->roomid ) ) {
$checked_values = $fromform->roomid;
foreach ( $checked_values as $val ) {
//Räume auslesen
$rooms = $DB->get_record_sql( "SELECT * FROM {bs_steinweg} WHERE id = $val" );
if ( $rooms->userid == $USER->id ) {
$DB->delete_records( 'bs_steinweg', [ 'id' => $val ] );
} else {
$getDeleteCanceld = get_string( 'noDeleteRights', 'local_buchungssystem' );
redirect( $CFG->wwwroot . '/local/buchungssystem/meine_buchungen.php', $getDeleteCanceld );
}
}
$getDeleteCanceld = get_string( 'getDeleteCanceld', 'local_buchungssystem' );
redirect( $CFG->wwwroot . '/local/buchungssystem/meine_buchungen.php', $getDeleteCanceld );
}
}
2
Answers
My solution:
The processing:
You need to make sure, that you’re assigning unique names to your input fields. In this case you add a hidden input field using
with the name
id
and the value$room->id
. Then for the next room in your for loop the nameid
is overwritten, since you use the same name as for the previous room. You could for example create unique names by doing this:The same goes for your
advcheckbox
fields.Your revised form code:
You’d need to test if your deletion code works or if you need to tweak it regarding the changes. I also highly advise you to read more about the usage of
advcheckbox
in the docs here. There you can also find more info about thename
parameter and how to read the data received.