Q:

CodeIgniter: How to Insert data with one name and multiple selectbox into the second table?

There is a registration form and there are 5 fields on that form. One of them is adding hobbies. This field is a selectbox and when a user wants to add more hobbies more than one, there is a + button to generate a new selectbox for adding the second one and etc. 
As an example: 
  Name:
  Surname:
+ Hobbies: Select One
           -fishing
           -shopping
           -etc.

When the plus is clicked, a new selectbox is occured. 
  Name:
  Surname:
  Hobbies: fishing
           -shopping
           -etc.
+ Hobbies: Select One
           -shopping
           -etc.

So the code for selectbox is below.
My View File
<?php
    echo '<select name="hobby_id[]" class="select2" data-allow-clear="true" data-placeholder="Select One">';
         echo '<option></option>';
         echo '<optgroup label="Hobby List">';
                foreach($hobbies as $hobby_item)
                {
                    echo '<option value="'.$hobby_item->hobby_id.'">'.$hobby_item->hobby_name.'</option>';
                }
         echo '</optgroup>';
    echo '</select>';
?>

I want to insert data from multiple selecbox has the same name. But in my model file, I am not sure how to use it. My model.php
$this->db->set('name',  $this->input->post('name')); 
$this->db->set('surname',   $this->input->post('surname')); 
$this->db->set('phone',     $this->input->post('phone')); 
$this->db->set('mail',  $this->input->post('mail')); 

$this->db->insert('users'); 

$this->db->set('user_id',  $this->db->insert_id());
$this->db->set('hobby_id', $this->input->post('hobby_id'));

$this->db->insert('hobbies');

The view I am trying to do is like below. 
user_id | hobby_id
   1          1
   2          1
   2          2

I want to add multiple row with the selectbox that has same name. But I don't know how to write in the model file. Could you help me? Could you show me the trick I can search on the internet or you can share if you have a simple example.

A:

You will get array of values for Hobbies. 
So for Hobbies try this in your model after your user insert query :
$insert_id = $this->db->insert_id();
$hobby_ids = $this->input->post('hobby_id');
if(count($hobby_ids) > 0){
  for($i=0;$i<count($hobby_ids);$i++) {
    $this->db->set('user_id', $insert_id );
    $this->db->set('hobby_id',$hobby_ids[$i]);
    $this->db->insert('hobbies');
  }
}

