I am having trouble updating my value in my database on form submit. I use codeigniter 2.20
I am getting a error Fatal error: Call to undefined method Model_setting::updateTheme() in E:\\Xampp\\htdocs\\codeigniter-theme\\admin\\controllers\\setting\\setting.php on line 8
What I am trying to archive is on once have selected theme in form it will update the setting table value is where it gets posted to. It's not changing on form submit either. I have autoloaded form_validation lib and form helper.
Model
<?php
class Model_setting extends CI_Model {
public function updateTheme() {
$this->db->select('*');
$this->db->where('group', 'config');
$this->db->where('key', 'config_template');
$this->db->where('value', $this->input->post('config_template')); // Need to update theme row
$query = $this->db->update('setting');
}
}
view
<form method="post" action="<?php echo $action;?>" role="form" class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="input-template"><?php echo $entry_template; ?></label>
<div class="col-sm-10">
<select name="config_template" id="input-template" class="form-control">
<?php foreach ($templates as $template) { ?>
<?php if ($template == $config_template) { ?>
<option value="<?php echo $template; ?>" selected="selected"><?php echo $template; ?></option>
<?php } else { ?>
<option value="<?php echo $template; ?>"><?php echo $template; ?></option>
<?php } ?>
<?php } ?>
</select>
<br />
<img src="" alt="" id="template" class="img-thumbnail" />
</div>
</div>
<button type="submit" class="btn btn-md btn-primary">Save</button>
</form>
Controller
public function index() {
$this->load->model('setting/model_setting');
$this->model_setting->updateTheme();
if(null !==($this->input->post('config_template'))) {
$data['config_template'] = $this->input->post('config_template');
} else {
$data['config_template'] = $this->theme->get('value'); // Auto loaded Library Theme
}
$data['templates'] = array();
$directories = glob(DIR_CATALOG . 'views/theme/*', GLOB_ONLYDIR);
foreach ($directories as $directory) {
$data['templates'][] = basename($directory);
}
$this->form_validation->set_rules('config_template', '', 'callback_validate');
if($this->form_validation->run()) {
redirect('setting/store');
} else {
$this->lang->load('setting/setting', 'english');
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('text_home'),
'href' => site_url('common/dashboard')
);
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('heading_title'),
'href' => site_url('setting/setting')
);
$data['action'] = site_url('setting/setting');
$data['title'] = "Settings";
$data['entry_template'] = $this->lang->line('entry_template');
$data['header'] = $this->header($data);
$data['footer'] = $this->footer($data);
$this->load->view('setting/setting', $data);
}
}
I have got it working.
Model
<?php
class Model_setting extends CI_Model {
public function update_theme() {
$this->db->where('key', 'config_template');
$this->db->set('value', $this->input->post('config_template'));
$this->db->update('setting');
}
}
controller
public function index() {
//$this->load->model('setting/model_setting');
$data['templates'] = array();
$directories = glob(DIR_CATALOG . 'views/theme/*', GLOB_ONLYDIR);
foreach ($directories as $directory) {
$data['templates'][] = basename($directory);
}
$this->form_validation->set_rules('config_template', '', 'callback_validate');
if($this->form_validation->run() == true) {
$this->load->model('setting/model_setting');
if($this->model_setting->update_theme()) {
$data['config_template'] = $this->input->post('config_template');
} else {
$data['config_template'] = $this->input->get('config_template');
}
redirect('setting/store');
} else {
$this->lang->load('setting/setting', 'english');
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('text_home'),
'href' => site_url('common/dashboard')
);
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('heading_title'),
'href' => site_url('setting/setting')
);
$data['action'] = site_url('setting/setting');
$data['title'] = "Settings";
$data['entry_template'] = $this->lang->line('entry_template');
$data['header'] = $this->header($data);
$data['footer'] = $this->footer($data);
$this->load->view('setting/setting', $data);
}
}
You should read some basic CI first:
function index() {
$this->load->model('setting/model_setting'); //load model
if( $this->input->post(null) ){ //detect if form is submitted
if(null !==($this->input->post('config_template'))) {
$data['config_template'] = $this->input->post('config_template');
} else {
$data['config_template'] = $this->theme->get('value'); // Auto loaded Library Theme
}
if($this->form_validation->run()) {
$this->model_setting->updateTheme();
}
redirect('setting/store');
}else{ // load view only
$data['templates'] = array();
$directories = glob(DIR_CATALOG . 'views/theme/*', GLOB_ONLYDIR);
foreach ($directories as $directory) {
$data['templates'][] = basename($directory);
}
$this->form_validation->set_rules('config_template', '', 'callback_validate');
$this->lang->load('setting/setting', 'english');
$data['breadcrumbs'] = array();
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('text_home'),
'href' => site_url('common/dashboard')
);
$data['breadcrumbs'][] = array(
'text' => $this->lang->line('heading_title'),
'href' => site_url('setting/setting')
);
$data['action'] = site_url('setting/setting');
$data['title'] = "Settings";
$data['entry_template'] = $this->lang->line('entry_template');
$data['header'] = $this->header($data);
$data['footer'] = $this->footer($data);
$this->load->view('setting/setting', $data);
}
}
You should first check if there is any post request, if there is update and redirect else show the form.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.