1

I have application where user can select multiple items from html selet and this selection need store as one id row,

Ex :

time_id   time_name   values

1         common     x,y,z

Below is my PHP controller,

$time_data=array(
    'time_name'=>$this->input->post('time_name'),
    'time_days'=>$this->input->post('time_days'),
    'time_hours'=>$this->input->post('time_hours'),
    'time_minutes'=>$this->input->post('time_minutes'),
    'time_start'=> implode(",", $this->input->post('time_start')),
    'time_end'=>implode(",", $this->input->post('time_end')),
    'time_department'=>implode(",", $this->input->post('time_department')),
    'time_timecategory'=>$this->input->post('time_timecategory'),
    'time_searchwords'=>$this->input->post('time_searchwords'),
    'timecreated_time' =>date("Y-m-d H:i:s")
    );

//Add starts
if($this->form_validation->run() !== FALSE) {
    $result = $this->model_admin->updatetime($time_data);

    if(!$result) {
        $content = $this->model_admin->LastEntrytime();
        echo json_encode($content);
    }
}
else {
    echo json_encode(array('cival'=>0, 'val_message' => validation_errors()));
}

Below is my HTML where user can select multiple items from dropdown,

<div class="col-md-9">
  <select id="time_department" name="time_department[]" class="form-control select2" multiple>
<?php
  foreach($departments_array as $department) { ?>
    <option value="<?php echo $department["department_id"];?>"><?php echo $department["department_name"]?></option>
<?php
  } ?>
  </select>
</div>

With this my multiselected values are stored as x,y,z or 1,2,4 etc...

But i want to create many-to-many linked table in which i will store values of x,y,z in each different row instead of comma seperated values,

How can i insert multiple selected values in mysql as many to many table?

Thanks,

1 Answer 1

0

Not sure if I quite understood your problem. See if this helps:

1) First of all, create a new table to store the values that will contain the values selected.

2) What is the primary key of this table? Is it time_id? If it doesn't have a PK, then create one.

3) In the new table you just created, add a foreign key that references the primary key of the first existing table. Also make that field/column not null.

4) Add as many rows as necessary (according to how many values are selected) in the just created table. Don't forget to link them back to the correct row in the first table using the FK.

Is that what you were trying to achieve?

Sign up to request clarification or add additional context in comments.

2 Comments

Yes nearly same, time_id is PK, making little more clear, i need to create one row with time_id will be 1, this row have time_name = test and time_values = good, bad, soso, blabla. or may be like 2,4,5,10 which are IDs of other table(FK), now storing with 2,4,5 is not good idea, so i need to create row for each multi select values in new table? which have structure like 1, good, 2 -- 1, bad, 4 -- 1,soso,5 -- 1,blabla-10 like this so instead of storing as comma seperated, i will store in table, how to do that?
Something like this, stackoverflow.com/questions/4804841/…, this have ids in columns, but will have FKs + strings as well,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.