php - Model not updating correct -



php - Model not updating correct -

when update database row. reason update values same lastly input strange.

how can working rows not updated value same?

this how database works. $group = 'config'. $key = example:'config_name'. $value = ever typed in $key.

in model utilize foreach ($data $key => $value) { unusual updates values lastly input value.

<?php if ( ! defined('basepath')) exit('no direct script access allowed'); class model_website_setting extends ci_model { public function editwebsite($group , $data, $website_id = 0) { foreach ($data $key => $value) { // create sure keys belonging grouping used if (substr($key, 0, strlen($group)) == $group) { if (!is_array($value)) { $this->db->set('group', $group); $this->db->set('value', $key); $this->db->set('value', $value); $this->db->set('website_id', $website_id); $this->db->update($this->db->dbprefix . 'setting'); } else { $this->db->set('group', $group); $this->db->set('value', $key); $this->db->set('value', $value); $this->db->set('website_id', $website_id); $this->db->update($this->db->dbprefix . 'setting'); } } } } }

controller

<?php if ( ! defined('basepath')) exit('no direct script access allowed'); class website_settings extends controller { public function __construct() { parent::__construct(); $this->lang->load('admin/setting/setting', 'english'); } public function index() { $this->document->settitle($this->lang->line('heading_title')); $data['entry_name'] = $this->lang->line('entry_name'); $data['entry_owner'] = $this->lang->line('entry_owner'); if (!empty($this->input->post('config_name'))) { $data['config_name'] = $this->input->post('config_name'); } else { $data['config_name'] = $this->settings->get('config_name'); } if (!empty($this->input->post('config_owner'))) { $data['config_owner'] = $this->input->post('config_owner'); } else { $data['config_owner'] = $this->settings->get('config_owner'); } $this->load->library('form_validation'); $this->form_validation->set_rules('config_name', 'website name'); $this->form_validation->set_rules('config_owner', 'your name'); if ($this->form_validation->run() == false) { homecoming $this->load->view('setting/website_settings', $data); } else { $this->load->model('admin/setting/model_website_setting'); $this->model_website_setting->editwebsite('config', $this->input->post()); $this->session->set_flashdata('success', 'you have updated settings!'); redirect('admin/setting/website'); } } }

if utilize built in update_batch , not place update on every loop.

foreach ($data $key => $value) { // create sure keys belonging grouping used if (substr($key, 0, strlen($group)) == $group) { if (!is_array($value)) { // logic // if value same not insert in update array // if value not same insert on update array $update[] = array( 'group' => $group, 'value' => $key, 'website_id' => $website_id ); } else { $update[] = array( 'group' => $group, 'value' => $key, 'website_id' => $website_id ); } } } $this->db->update_batch($this->db->dbprefix . 'setting', $update , 'website_id');

sample on how utilize update batch on code not tested.

php codeigniter

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -