I currently have a multi select box which functions properly until there is one last item which does not delete from the database when deselected. For some reason all but 1 item is removed.
$domain_records = $DB->get_records('tag', ['tagtype' => 'domain'], $sort = 'rawname', '*');
if(!empty($_POST["domain_list"]) || isset($_POST["domain_list"])){
$posted_domains = array();
$posted_domains = $_POST["domain_list"];
// elseif logic submits newly selected tags
foreach($domain_records as $domain_record) {
// already selected and still selected
if(in_array($domain_record->id, $posted_domains) === true &&
$DB->record_exists('tcors_journal_tags_matrix', array('journal_publication_id' => $pubid, 'tag_id' => $domain_record->id))) {
continue;
}
// selected and no longer selected on post
else if(in_array($domain_record->id, $posted_domains) === false &&
$DB->record_exists('tcors_journal_tags_matrix', array('journal_publication_id' => $pubid, 'tag_id' => $domain_record->id))) {
// DELETE ME!!!!
$DB->delete_records("tcors_journal_tags_matrix", array('journal_publication_id' => $pubid, 'tag_id' => $domain_record->id));
}
else if(empty($posted_domains)){
$DB->delete_records("tcors_journal_tags_matrix", array('journal_publication_id' => $pubid, 'tag_domain' => "Domain"));
//$DB->delete_records("tcors_journal_tags_matrix", array('journal_publication_id' => $pubid, 'tag_id' => $domain_record->id));
}
else if(in_array($domain_record->id, $posted_domains) === true) {
$tag_matrix = new \stdClass();
$tag_matrix->journal_publication_id = $pubid;
$tag_matrix->tag_id = $domain_record->id;
$tag_matrix->tag_domain = 'Domain';
$tag_matrix->create_date = time();
$tag_matrix->create_by = $USER->id;
$tag_matrix->modify_date = time();
$tag_matrix->modify_by = $USER->id;
$DB->insert_record('tcors_journal_tags_matrix', $tag_matrix);
}
}
}
My attempt to account for a scenario where the $posted_domains array (which contains the values to be submitted to the db) is the 2nd to last else if statement. I would like all items deleted when the post back array ($posted_domains) contains no values.