2016-07-13 27 views
0

我想創建遞歸表選中複選框基本ID,我喜歡這個顯示選中的複選框基地遞歸ID在笨

+---------+--------------+-----------+ 
|user_id | function_id | feature_id| 
+---------+--------------+-----------+ 
| 1  | 2   | 1  | 
+---------+--------------+-----------+ 
| 1  | 2   | 2  | 
+---------+--------------+-----------+ 
| 1  | 2   | 3  | 
+---------+--------------+-----------+ 

,然後選中複選框表使用的foreach

$feature = $this->db->get_where('my_table', array('function_id' => '2'))->result(); 
foreach ($result as $value) 
{ 
$data = array(
     'name'   => 'feature_id', 
     'value'   => $value->feature_id, 
     'checked'  => // depend on feature_id if in function_id the feature id is exist then checked TRUE else FALSE 
); 

echo form_checkbox($data); 

PLZ有幫我! :(

+0

可能是重複的http://stackoverflow.com/questions/8878986/how-to-set-value-of-checkbox-in-codeigniter-and-show-checked-state – siddhesh

+0

@yulianto你可以請檢查我的答案請讓我知道,如果它在爲你工作嗎? –

回答

1
如果你想顯示

改變你的php這樣的代碼,讓我知道。

$feature = $this->db->get_where('my_table', array('function_id' => '2'))->result(); 

foreach ($result as $value) { 

    $state = (!empty($value->feature_id)) ? TRUE : FALSE; 

    $data = array('name' => 'feature_id', 
        'value' => $value->feature_id, 
        'checked' => $state); 

    echo form_checkbox($data); 
} 

編輯

所有checkboxs那麼它一定是你foreach循環中。

+0

它不工作先生,我有另一個問題,這是簡單的,但我仍然不知道答案大聲笑, 例如,假設我有一個數據,如在表1,2,3,4, 5然後我循環使用foreach的數據,我想在複選框中選擇2,3和5,如何創建這樣的?請幫幫我!! –

+0

需要更多信息在這裏。你的表中的行是有限的嗎?或者有n行?簡而言之,複選框是有限的? –