2017-05-31 102 views
1

我有一个Choice列表包含4种类型的文章。用户应该选择一个。现在列表变成包含45篇文章,我将choice list更改为checkBox list以供多种选择。检索数据复选框-php

这因循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'; 
    } 

如何我可以改变这个二传手功能recovre不写所有的45种物品中的数据?

回答

1

你可以自动执行允许的性质,像这样的期待:

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

唯一消极的一面是,你需要存储的允许性质的地方,在这种情况下,这将是一个数组,但它可能是从你的数据库以及。

根据您当前的信息,这是我能做到的!

+0

谢谢你的有用的答案。有效的是,45条款存储在我的数据库中的表格中。 – Nazly

+1

如果您检索它们,您可以将它们设置为一个数组,这样您就不必再次更新它,只需使用以逗号分隔的自然列表即可,此代码将起作用。 – FMashiro

0
<form method='post' id='userform' action='thisform.php'> <tr> 
    <td>Trouble Type</td> 
    <td> 
    <input type='checkbox' name='checkboxvar[]' value='Option One'>1<br> 
    <input type='checkbox' name='checkboxvar[]' value='Option Two'>2<br> 
    <input type='checkbox' name='checkboxvar[]' value='Option Three'>3 
    </td> </tr> </table> <input type='submit' class='buttons'> </form> 

<?php 
if (isset($_POST['checkboxvar'])) 
{ 
    print_r($_POST['checkboxvar']); 
} 
?> 

希望这有助于

echo implode(',', $_POST['checkboxvar']); // change the comma to whatever separator you want 
+0

你能解释一下,这将如何阻止他编辑setter以拥有45个以上的elseif? – FMashiro

+0

你是对的,对不起,我没有明白这个问题 –

+0

没有问题,但这可能会帮助OP获得所有的名字一次放入数组或东西:) – FMashiro