Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Updating Database
#1

[eluser]riwakawd[/eluser]
I am making a form which lets me select what theme I would like to use and it should update the database and then update my config file.

In my config file I have made $config['config_template'] = $template;

Config File Setting.php

Code:
<?php

// $config['config_template'] = "default";

$config['config_template'] = $template;

I am trying to get it so when I choose what theme I am after it will update the database with value i.e name of theme.

And make it so the config item stays on the theme selected from database.

How do I make it work like that? I can see the themes fine in select form.

Controller
Code:
<?php

class Setting extends CI_Controller {

public function index() {
  $this->config->item('setting');
  $this->load->model('setting_model');

  if (null !==($this->input->post('config_template'))) {
   $data['config_template'] = $this->input->post('config_template');
  } else {
   $data['config_template'] = $this->config->set_item('config_template', $template);
  }

  $data['templates'] = array();

  // DIR_APPLICATION is defined As APPPATH FOR MULTI CI INSTALL.

  $directories = glob(DIR_APPLICATION . 'views/theme/*', GLOB_ONLYDIR);

  foreach ($directories as $directory) {
   $data['templates'][] = basename($directory);
  }

  return $this->load->view('setting/setting', $data);
}
}

Model
Code:
<?php

class Setting_model extends CI_Model {

public function editSetting() {
  $this->db->set('group', 'config');
  $this->db->set('key', 'config_template');
  $this->db->set('value', $this->input->post('config_template'));
  $this->db->update($this->db->dbprefix, 'setting');
}

}

}

View
Code:
<form method="post" acti echo base_url('setting');?>">
  
<div class="form-group">
<label class="col-sm-2 control-label" for="input-template">Template</label>
<div class="col-sm-10">
<select name="config_template" id="input-template" class="form-control">
&lt;?php foreach ($templates as $template) { ?&gt;
&lt;?php if ($template == $config_template) { ?&gt;
<option value="&lt;?php echo $template; ?&gt;" selected="selected">&lt;?php echo $template; ?&gt;</option>
&lt;?php } else { ?&gt;
<option value="&lt;?php echo $template; ?&gt;">&lt;?php echo $template; ?&gt;</option>
&lt;?php } ?&gt;
&lt;?php } ?&gt;
</select>
</div>
</div>

<div class="form-group">
<button type"submit" value="" class="btn btn-md btn-primary">Save</button>
</div>
  
&lt;?php echo form_close();?&gt;
#2

[eluser]Tim Brownlaw[/eluser]
Where would you prefer to save your current theme setting?

The Database OR the Config file.. Having both is silly because, in your script where does it decide where it should go and look?

You can of course update the config setting (Not the config file setting ) from the Database each time a page is loaded.

In your database you would have an options or settings table which you can read in each time and store in memory and write a method to retrieve those kinds of thing from it... That way it's only one DB Select instead of performing a DB Select each time you need it throughout the current script. That's beneficial for things you need to know more than once during the running of the script.

You could have it so if there's been no setting or a valid setting in the options table, to go and read the config setting as the default OR better is to set the actual default theme in the options table on installation (preferred method).

There's a bunch of ways to do things.

But I see no point in setting a value in the DB AND rewriting your config file for a theme setting... Unless you have some reason I cannot see!

Check your model - I see one too many } (on the end)
Check your view - The first line is wrong! Typo on action and incomplete &lt;?php .... ?&gt;

#3

[eluser]riwakawd[/eluser]
[quote author="Tim Brownlaw" date="1405328252"]Where would you prefer to save your current theme setting?

The Database OR the Config file.. Having both is silly because, in your script where does it decide where it should go and look?

You can of course update the config setting (Not the config file setting ) from the Database each time a page is loaded.

In your database you would have an options or settings table which you can read in each time and store in memory and write a method to retrieve those kinds of thing from it... That way it's only one DB Select instead of performing a DB Select each time you need it throughout the current script. That's beneficial for things you need to know more than once during the running of the script.

You could have it so if there's been no setting or a valid setting in the options table, to go and read the config setting as the default OR better is to set the actual default theme in the options table on installation (preferred method).

There's a bunch of ways to do things.

But I see no point in setting a value in the DB AND rewriting your config file for a theme setting... Unless you have some reason I cannot see!

Check your model - I see one too many } (on the end)
Check your view - The first line is wrong! Typo on action and incomplete &lt;?php .... ?&gt;

[/quote]

The typo on view in form is to do with in here won't let me put full action.

I would like to be able to submit form then it updates the row in database and then sets the config item.

Below here is how I do the front controller to load view. I can do it manually buy that I mean change name on my config file OK just would like to do it via form.

Code:
&lt;?php

class Home extends CI_Controller {
       // Dir Template is defined also know as APPPATH just makes it easy to find location when have multi CI install.
public function index() {
  if(file_exists(DIR_TEMPLATE . $this->config->item('config_template') . '/template/common/home.php')) {
   $this->load->view($this->config->item('config_template') . '/template/common/home');
  } else {
   $this->load->view('default/template/common/home');
  }
}
}
#4

[eluser]riwakawd[/eluser]
[quote author="Tim Brownlaw" date="1405328252"]Where would you prefer to save your current theme setting?

The Database OR the Config file.. Having both is silly because, in your script where does it decide where it should go and look?

You can of course update the config setting (Not the config file setting ) from the Database each time a page is loaded.

In your database you would have an options or settings table which you can read in each time and store in memory and write a method to retrieve those kinds of thing from it... That way it's only one DB Select instead of performing a DB Select each time you need it throughout the current script. That's beneficial for things you need to know more than once during the running of the script.

You could have it so if there's been no setting or a valid setting in the options table, to go and read the config setting as the default OR better is to set the actual default theme in the options table on installation (preferred method).

There's a bunch of ways to do things.

But I see no point in setting a value in the DB AND rewriting your config file for a theme setting... Unless you have some reason I cannot see!

Check your model - I see one too many } (on the end)
Check your view - The first line is wrong! Typo on action and incomplete &lt;?php .... ?&gt;

[/quote]

I have almost got it working now can update database model just need now to make my $store_info['config_template'] to work with the config item.

And also just have to work out how to make it row id to stop changing but just to be fixed.

Code:
&lt;?php

class Setting extends CI_Controller {

public function index() {

  $this->load->model('setting_model');

  if ($this->input->server('REQUEST_METHOD') == 'POST') {

$this->setting_model->editSetting('config', $this->input->post(), $store_id);

   redirect('setting');

  }

if (null !==($this->input->post('config_template'))) {
$data['config_template'] = $this->input->post('config_template');
} elseif (null !==($store_info['config_template'])) {
$data['config_template'] = $store_info['config_template'];
} else {
$data['config_template'] = '';
}

$data['templates'] = array();

$directories = glob(DIR_APPLICATION . 'views/theme/*', GLOB_ONLYDIR);

foreach ($directories as $directory) {
$data['templates'][] = basename($directory);
}

return $this->load->view('setting/setting', $data);
}

public function validate() {

}
}

Model edit part

Code:
public function editSetting($group, $data, $store_id = 0) {
$this->db->query("DELETE FROM " . $this->db->dbprefix . "setting WHERE store_id = '" . (int)$store_id . "' AND `group` = " . $this->db->escape($group) . " ");

foreach ($data as $key => $value) {
if (!is_array($value)) {
$this->db->query("INSERT INTO " . $this->db->dbprefix . "setting SET store_id = '" . (int)$store_id . "', `group` = " . $this->db->escape($group) .", `key` = " . $this->db->escape($key) . ", `value` = ". $this->db->escape($value) ." ");
} else {
$this->db->query("INSERT INTO " . $this->db->dbprefix . "setting SET store_id = '" . (int)$store_id . "', `group` = " . $this->db->escape($group) . ", `key` = " . $this->db->escape($key) . ", `value` = ". $this->db->escape(serialize($value)) . ", serialized = '1'");
}
}
}




Theme © iAndrew 2016 - Forum software by © MyBB