0

I am Inserting a multiple form input data in to database using Codeigniter. I have this post input array:

 Array
(
 [subject_id] => Array
    (
        [0] => 1
        [1] => 1
    )

[question] => Array

    (
        [0] => test
        [1] => test2
    )

[option1] => Array
    (
        [0] => test
        [1] => test2
    ) )

I don't get that how do i convert this array to insert How to insert this array using Insert batch.

$this->db->insert_batch('mytable', $data);

This is the form code which i use for posting the data:

<form method="post">
    <input type="text" name="subject_id[]" >
    <input type="text" name="question[]" >
    <input type="text" name="record[]" >

    // Down side Part is appended when user want to add more question

    <input type="text" name="subject_id[]" >
    <input type="text" name="question[]" >
    <input type="text" name="record[]" >

    <input type="submit" name="submit" >
</form>

Below is the Array format which i want.

$data = array(
    array(
        'subject_id' => 'My title' ,
        'question' => 'My Name' ,
        'option1' => 'My date'
        ),
    array(
        'subject_id' => 'Another title' ,
        'question' => 'Another Name' ,
        'option1' => 'Another date'
        )
    );
4
  • post your code in your question regarding how do you get this array? Commented Mar 27, 2017 at 6:45
  • Sir I Have updated the question. @dhruvjadia Commented Mar 27, 2017 at 7:31
  • I have updated the question again sir & added what the result array i need. @dhruvjadia Commented Mar 27, 2017 at 15:43
  • provided answer Commented Mar 28, 2017 at 3:56

2 Answers 2

5
<?php
    $i = 0;
    foreach($subject_id as $key=>$val)
    {
          $data[$i]['subject_id'] = $val;
          $data[$i]['question'] = $question[$key];
          $data[$i]['option1'] = $record[$key];
          $i++;
    }
    $this->db->insert_batch('mytable', $data);

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

Comments

2

Try like below:Assume $records is an array that you want to insert.

foreach ($records as $record) 
{

    for ($i=0; $i < count($record); $i++) 
    {   
    $data[$i]['subject_id'] = $record['subject_id'][$i];
    $data[$i]['question'] = $record['question'][$i]
    $data[$i]['option1'] = $record['option1'][$i];
    }

}

Then

$this->db->insert_batch('mytable', $data);

4 Comments

are you getting value of $records['subject_id'][0] properly.
what is the name of array that you are posting in question.
I give it the $records = array(); and so on.
I have updated the question again sir & added what the result array i need. @hekmat

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.