0
votes

I want to update my table where the input the same input fields names are array and has add more function which generates the input fields like this:

I want to do update_batch in codeigniter my model i created a function like this: This is the code block:

    function update_batch_all($tblname,$data=array(),$userid)
    {
       $this->db->trans_start();
       $this->db->where('userid',$userid);
       $this->db->update_batch($tblname,$data);
       $this->db->trans_complete();
       return TRUE;
    }

it is not working. can any one help me that how can i update tables data with update batch that has where condition?

2

2 Answers

1
votes

You can read the docs for update_batch() here

Here's the short summary:

You pass in an associative array that has both, your where key, and the update value. As the third parameter to the update_batch() call, you specify which key in your assoc array should be used for the where clause.

For example:

$data = array(
  array(
    'user_id' => 1,
    'name' => 'Foo'
  ), array(
    'user_id' => 2,
    'name' => 'Bar'
  )
);

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

Breakdown of arguments passed:$tbl is the table name. $data is the associative array. 'user_id' tells CI that the user_id key in $data is the where clause.

Effect of the above query: Name for user with user_id = 1 gets set to Foo and name for user with user_id=2 gets set to Bar.

In your case, if you want to set the same user_id key in each array with your data array, you can do a quick for loop:

foreach ($data as &$d) { 
  $d['user_id'] = $user_id;
}


$this->db->update_batch($tbl, $data, 'user_id');
0
votes
You can use,

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

But all array within data must have a field 'user_id'

eg:

    $data=array( 

               array('user_name'=>'test1','user_id'=>1),
               array('user_name'=>'test2','user_id'=>2)

                  );

You can get more details about update_batch from here