我有一个Choice列表包含4种类型的文章。用户应该有一个选择。现在这个列表包含45篇文章,我将choice list改为checkBox list进行多项选择。
这遵循旧的setter函数:

 public function setNature($nature) {
        $this->_nature = $nature;
        if ($nature === 'Production') { $this->_nature='Production';   }
        elseif ($nature === 'A'){ $this->_nature='A';}
        elseif ($nature === 'B') {$this->_nature='B';}
        else $this->_nature ='all';
    }

如何更改此setter函数以重新记录数据,而不必编写所有45种类型的文章?

最佳答案

您可以自动查找允许的属性,例如:

public function setNature($nature) {
    $allowed_nature = ['A','B','Production'];
    if(in_array($nature, $allowed_nature)){
        $this->_nature = $nature;
    }else{
        $this->_nature = 'all';
    }
}

唯一不利的一面是,您需要将允许的属性存储在某个地方,在本例中,这将是一个数组,但它也可能来自您的数据库。
根据你目前的信息,这是我能做的!

关于php - 从复选框中检索数据-PHP,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44283268/

10-14 23:19
查看更多