2

I want to update a multiple row using

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

But Don't Know How to give condition in this update_batch. Can I use this where?

$this->db->where('id', Multiple Ids Here); 
mayur panchal
  • 265
  • 1
  • 16
  • see this answer: https://stackoverflow.com/questions/16790749/codeigniter-update-batch-with-included-update-of-the-where-key – Pathik Vejani Jun 01 '17 at 12:10
  • what will be the normal mysql query for this ??? – Swarna Sekhar Dhar Jun 01 '17 at 18:32
  • @SwarnaSekharDhar You can lookup this links for more help with sql multiple row update. https://stackoverflow.com/questions/20255138/sql-update-multiple-records-in-one-query –  Jun 03 '17 at 04:10

2 Answers2

2

i have code that is different from yours but it works and also it is easy to understand first put this function in your model file

in model file

function updatedata($tbname, $data, $parm)
    {
        return $this->db->update($tbname, $data, $parm);    
    }

in your controller file use like this

  $this->load->model("Your_model_name");    
  $this->Your_model_name->updatedata('mytable',$data,$your_condition_in_array);

i hope this will help, i am sure about that this code works, please let me know that this code is worked for you.

Astound
  • 192
  • 12
  • This query in model is for only one row to update but i want to update multiple rows. I do not want to use loop for updating row one by one. Reply me if i misunderstood or totally get it wrong. @Astound –  Jun 03 '17 at 04:07
  • i have not tried with multiple update with this function but you can just try put your all data in array, and put all array data into single array and pass it to that function `$main_array=array(array(your fields with value),array(your fields with value))` i hope it will works – Astound Jun 03 '17 at 10:03
1

Per the CodeIgniter documentation on update_batch()

$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name 2' ,
      'date' => 'My date 2'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name 2' ,
      'date' => 'Another date 2'
   )
);

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

The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.

In other words, The array contains all data for the fields to be updated plus the item used to define the "where" condition. In the above arrays the value associated with the "title" key is used. So two records are updated: One where title = 'My title' and the second where title = 'Another title'

update_batch() returns the number of rows affected.

DFriend
  • 8,869
  • 1
  • 13
  • 26
  • I have read that first but i don't get that how do i put give my ids in where condition that which rows i want to update. @Dfriend –  Jun 03 '17 at 04:08
  • I added some more details to the answer. Hope it makes it clear what is happening. – DFriend Jun 05 '17 at 13:02