1

I'm using codeigniter, below is my array. How do I insert the data into a database with column name sibbling_name and sibbling_age.

I tried a CI tutorial but it showed me an error because 0,1,2 is not field in db. Of course is it how to change that [0] into 'sibbling_name' column and [0] in sibblingAge into 'sibbling_age' column?

[sibblingName] => Array
    (
        [0] => Ryan Yaohari
        [1] => Rico Yaohari
        [2] => Rino Yaohari
    )

[sibblingAge] => Array
    (
        [0] => 23
        [1] => 21
        [2] => 19
    )
3
  • What have you tried so far? What have you done that works or doesn't work? Show us the code from your relevant model and you're more likely to get help here. Commented May 20, 2014 at 2:37
  • are sibblingName and sibblingAge separate arrays? Commented May 20, 2014 at 3:23
  • yes sibblingName and sibblingAge are seperate arrays Commented May 20, 2014 at 3:33

1 Answer 1

2

According to the manual, if you want an insert batch you need to use $this->db->insert_batch();. But before that you need to properly format your values first. Consider this example:

$sibblingName = array('Ryan Yaohari', 'Rico Yaohari', 'Rino Yaohari');
$sibblingAge = array(23, 21, 19);
$insert_values = array();
for($x = 0, $size = count($sibblingName); $x < $size; $x++) {
    $insert_values[$x] = array(
        'sibbling_name' => $sibblingName[$x],
        'sibbling_age' => $sibblingAge[$x],
    );
}
print_r($insert_values);
// format should be something like this:

// Array
// (
//     [0] => Array
//         (
//             [sibbling_name] => Ryan Yaohari
//             [sibbling_age] => 23
//         )
//     [1] => Array
//         (
//             [sibbling_name] => Rico Yaohari
//             [sibbling_age] => 21
//         )
//     [2] => Array
//         (
//             [sibbling_name] => Rino Yaohari
//             [sibbling_age] => 19
//         )
// )

// and of course in the end, use the insert_batch method.
$this->db->insert_batch('mytable', $insert_values);
Sign up to request clarification or add additional context in comments.

Comments

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.