1

I have a table example

ID | answer1 | answer2   | question

1  | abc   | ghi    | 

2  | def   | klm    |

I Wanna update column question by select ID This my code to get ID form table example

$query = $this->db->query('SELECT * FROM example');
    $data=array();
    foreach($query->result() AS $value){
        $data[] = $value->id;

    } 
$implode = implode(',',$data);
//get from post
$question= $this->input->post('question');
foreach($question AS $q){

        $data[]=array(
        'id' => $implode,
        'question' => $q
        );

        }
        $this->db->update_batch('example',$data,'id');
//I use multiple add filed
<button class="add_field_button2">Add More Fields</button>
<input type="text" name="question[]" value="<?= $rowk->question;?>">

When add 2 question for update the table onlu one row is changed

My data array

Array ( [0] => Array ( [id] => 1,2 [question] => question1 ? ) ) Array ( [0] => Array ( [id] => 1,2 [question] => question1 ) [1] => Array ( [id] => 107,108 [question] => question2 ) )
remote
  • 11
  • 3

2 Answers2

0

If you use the update_batch then your array format must be like this

Array(
    [0]=>Array(
            [id]=>1,
            [question] => question1 ?
        ),
    [1]=>Array(
        [id]=>2,
        [question] => question1 ?
    ),
    [2]=>Array(
        [id]=>107,
        [question] => question2
    )
)

I think your array format is not correct

Poonam Navapara
  • 186
  • 1
  • 9
-1

Hello there you can check my answer Codeigniter - Update Multiple Row With Select2 I think this will help you something, if it does hit a like button :-D . thanks just change this

foreach($question AS $q){

        $tempdata=array(
        'id' => $implode, // this must be a single value i think because by default update_batch third parm is work like "=" not "IN"  
        'question' => $q
        );
  array_push($data, $tempdata);
 }
        $this->db->update_batch('example',$data,'id');
Sunny Danu
  • 77
  • 1
  • 10
  • hey . can you little mention here what 's not working . because its the same thing that above dev. has mentioned. your array format was not right so its good to use array_push to manage your multi array if your are using update_batch in CI,.. you can check the reference link as well. both are same problem thanks. – Sunny Danu Nov 06 '18 at 06:31