2017-05-31 28 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