Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update multiple Rows in Codeigniter

I have been looking around but I have not found an answer yet. Kindly help if you know the answer.

How do you update multiple rows in CI?

In my MySQL:

I have column names:

ID, Settings Name, Settings Value ( Settings Name is Unique )

I have the ff Data:

ID = 1, Settings Name = "Hello" , Settings Value = "True"
ID = 2, Settings Name = "World", Settings Value = "Good"

and more ...

I also have a form that gets the Settings Value but I am not sure how to update it on the DB. How to update the True for the Hello being the Settings Name and update the Good for the World.

I heard about insert_batch() but is there an update_batch()?

like image 896
jsdecena Avatar asked Jan 08 '13 09:01

jsdecena


1 Answers

There is indeed an update_batch() method available in CodeIgniter already.

You can use it your example like so:

$data = array(
    array(
        'ID' => 1,
        'Settings Name' => 'Hello',
        'Settings Value' => 'True'
    ),
    array(
        'ID' => 2,
        'Settings Name' => 'World',
        'Settings Value' => 'Good'
    )
);
$this->db->update_batch('tableName', $data, 'id'); 

So what you have is an array of arrays, the children basically hold the data for each row in the database. The first parameter for update_batch() is the name of the database table, the second is the $data variable and the third is the column you want to use in the WHEN clause.

like image 90
cchana Avatar answered Nov 18 '22 23:11

cchana