2014-01-14 40 views
0

發佈的數據,包括多選擇欄是插入多選擇值到數據庫笨

Array ([id] => 16 [pAttr] => Array ([0] => Colour [1] => Size)) 

我試圖插入只有一個屬性的ID和第二排做一遍。

____________________ 
| id | attribute | 
-------------------- 
| 16 | Colour | 
| 16 | Size  | 

控制器方法

public function add_config_product() 
{ 
    $data['id'] = $this->input->post('id'); 
    $data['attribute'] = $this->input->post('pAttr'); 

    /*$data = array(

     'id' => $this->input->post('id'), 
     'attribute' => $this->input->post('pAttr') 
    );*/ 

    //print_r($data); 
    $this->inventory_model->add_config_product($data); 
} 

模型

public function add_config_product($data) 
{ 
    $this->db->insert('product_attr_value', $data); 
} 

回答

1

嘗試這種情況:

控制器:

public function add_config_product() 
{ 
    $data['id'] = $this->input->post('id'); 
    foreach ($this->input->post('pAttr') as $attribute): 
     $data['attribute'] = $attribute; 
     $this->inventory_model->add_config_product($data); 
    endforeach; 
} 

這將遍歷pAttr輸入數組並在每行中添加值。

0

如果傳遞值的數組,以在模型中插入的功能,則可以使用以下類型:

public function add_config_product($data) 
{ 
    $this->db->insert_batch('product_attr_value',$data); 
}