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()?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With