Update all table rows on database by using one query in codeigniter

I have table in my database named settings
and it have 3 Fields id, key, value
and i need to update all settings in one page using one query

this is my settings model

class Settings_model extends CI_Model{
    /*
    * Get global settings
    */
    public function get_global_settings() {
      $query  = $this->db->get('settings');
      $result = $query->result();
      return $result;
    }

    /* Update setting */
    public function update($data, $id) {
        $this->db->where('id', $id);
        $this->db->update('settings', $data);
        return true;
    }
}

and this is my controller

class Settings extends MY_Controller{
    public function __construct() {
        parent::__construct();

        //Access control
        if(!$this->session->userdata('logged_in')) {
            redirect('admin/login');
        }
    }

    public function index(){

        // Get Categories
        $data['settings'] = $this->Settings_model->get_global_settings();

        // Load View
        $data['main_content'] = 'admin/settings/index';
        $this->load->view('admin/layouts/main',$data);
    }

    public function edit($id) {
        // Calidation Rules
        $this->form_validation->set_rules('value','Name','trim|required|xss_clean');

        $data['settings'] = $this->Settings_model->get_group($id);


        if($this->form_validation->run() == FALSE) {
          // View
          $data['main_content'] = 'admin/settings/edit';
          $this->load->view('admin/layouts/main',$data);

        } else {
          // Create Articles Data Array
          $data = array (
                'value'    => $this->input->post('value')
          );

          // Articles Table Insert
          $this->Settings_model->update($data, $id);

          // Create message
          $this->session->set_flashdata('setting_saved', 'Your setting has been saved');

          // Redirect page
          redirect('admin/settings');
        }

    }

}

but it only update one row by id

i need to make a foreach loop to update all rows

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s