Welcome Guest, Not a member yet? Register   Sign In
Updating multiple rows of the database
#1

[eluser]behnampmdg3[/eluser]
Hello;

I want to use active record to update the database. The table i want to update has 2 columns:
1 - title
2 - event_id

I send the values I want to update in an array to the model like this:

Code:
Array
(
    [rectangular] => 16
    [square_one] => 2
    [square_two] => 4
    [square_three] => 20
    [square_four] => 18
)


How can I use update active record to update all these in 1 query?.

This is my function
Code:
public function edit_special_events($data)
   {
    foreach($data as $val=>$row)
     {
      $update_data = array('event_id' => $row);
      $this->db->where('title', $val);
      $this->db->update('sincity_special_events_banners', $update_data);
     }
   }

//This produces:

UPDATE `sincity_special_events_banners` SET `event_id` = '16' WHERE `title` =  'rectangular'
UPDATE `sincity_special_events_banners` SET `event_id` = '17' WHERE `title` =  'square_one'
UPDATE `sincity_special_events_banners` SET `event_id` = '2' WHERE `title` =  'square_two'
UPDATE `sincity_special_events_banners` SET `event_id` = '5' WHERE `title` =  'square_three'
UPDATE `sincity_special_events_banners` SET `event_id` = '1' WHERE `title` =  'square_four'

Thank you



Thanks
#2

[eluser]CroNiX[/eluser]
Look at update_batch() in the user guide for database.
#3

[eluser]behnampmdg3[/eluser]
[quote author="CroNiX" date="1355898582"]Look at update_batch() in the user guide for database.[/quote]Oh thanks for your help.




Theme © iAndrew 2016 - Forum software by © MyBB