1

I'm having an issue if somebody could help would be much appreciated.

<input type="checkbox" name="symbols[]" value="1" />
.... and this line repeats 10-15 times with a different value

model

$data = array(
array('symbol_id' => $this->input->post('symbols'))
);
return $this->db->insert_batch('symbols', $data);

now my problem is that is inserting only first value of the selected checkbox (in this case 1) and ignores everything else without inserting a new row of other checkboxes (like value 3, 5 and 9). Could you suggest me some options ?

note: want i want to achieve is to insert a new row for each checkbox, so i can join another table which holds the symbols images (the values of checkboxes representing the id's of images). If you have any other way around of how I could do this is very welcomed. Thank you

edit: this is var_dump:

array(7)

{

[0] => string(1) "8"

[1] => string(1) "9"

[2] => string(2) "10"

[3] => string(2) "11"

[4] => string(2) "12"

[5] => string(2) "13"

}
1

2 Answers 2

0

The data is being sent as an array of active values, so if you would check 1,3,5,10,20, in your $this->input->post('symbols'); array you will have array(1, 3, 5, 10, 20):

function InsertSymbols() {
    $data = array();
    foreach($this->input->post('symbols') as $symb) {
        $data[] = array('symbol_id' => $symb);
    }
    return $this->db->insert_batch('symbols', $data);
}
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your quick reply but is not working. is telling me Array to string conversion Filename: database/DB_active_rec.php and Unknown column 'Array' in 'field list' INSERT INTO default_menus_details (0, 1, 2, 3) VALUES (Array,Array,Array,Array) Line Number: 1267
don't worry about the name of the table, in my code is named different than what i posted in my example, i checked that already:)
Oh, please post the var_dump($this->input->post('symbols')) output in your question!
No problem mate, try to solve this next time yourself by var_dumping and analyzing the errors you get, you will learn much better :)
0

This code is for CI but you can use it in core as well just put insert query with in your foreach.

$symbol_arr = $this->db->escape_str($this->input->post('symbols'));
foreach ($symbol_arr as $k=> $val) {
        $DataARR = array( 
               "symbol" => $val,
                "created" => date('Y-m-d h:i:s A'),
        );
    $this->common_model->insert_batch($DataARR);
}

Comments

Your Answer

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