选择列表项未从数据库中删除

时间:2018-12-04 19:11:05

标签: php moodle

我目前有一个多重选择框,该框可以正常运行,直到最后一项没有被取消选择时不会从数据库中删除。由于某种原因,除1个项目外的所有项目均被删除。

$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);

     }

 }

}

我试图说明一个场景,其中$ posted_domains数组(包含要提交给db的值)是if语句倒数第二个。我希望在发回数组($ posted_domains)不包含任何值时删除所有项目。

0 个答案:

没有答案