Update database field with array in CodeIgniter Update database field with array in CodeIgniter codeigniter codeigniter

Update database field with array in CodeIgniter


There are a lot of questions here. Do you already have values in the database and you want to update them? Or do you want to put in new data every time? The answer depends on that.

What you want is the insert_batch() or update_batch() methods of the active record class (if that's what you're using for the db).

foreach($post_array as $key => $value){    $settings[] = array(        'setting' => $key,        'value' => $value    );}$this->db->insert_batch('your_db_table', $settings);

OR, for updates:

$this->db->update_batch('your_db_table', $settings, 'setting');

You could do a query to check for settings and do insert_batch or update_batch depending on if there are results. If you wanted to insert every time, you could delete the rows in the table before you do the insert. I wouldn't do that without a transaction, however.


So you want to store the array data in the database? You could do this

Model

foreach ($data as $key => $item){      $this->db->set ('setting', $key);      $this->db->set ('value', $item);      $this->db->insert ('table_name');}return ($this->db->affected_rows() > 0);

Controller

if ($this->RecruitmentSettings->Update($form_data)){ redirect('recruitment/success');}else{     echo "error";}