ethio
ethio

Reputation: 539

Inserting multi select values into database codeigniter

The data posted, including the multi select field is

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

I am trying to insert an id with one attribute only and do it again in second row.

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

The controller method

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);
}

Model

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

Upvotes: 0

Views: 2543

Answers (2)

Adarsh M Pallickal
Adarsh M Pallickal

Reputation: 821

If you pass an array of values to a function in model to insert, then you can use the following type :

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

Upvotes: 0

jtheman
jtheman

Reputation: 7491

Try this:

Controller:

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;
}

This will loop through the pAttr input array and add values in each rows.

Upvotes: 1

Related Questions