CodeIgniter Forums
Updating config items through a form? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Updating config items through a form? (/showthread.php?tid=38368)



Updating config items through a form? - El Forum - 02-06-2011

[eluser]Joshee[/eluser]
I have been working on making my own forum with CodeIgniter and I have been trying to update config items through a form (For the admin. Updating items such as the site name, logo image, etc.)

For some reason, the form runs correctly, but the items don't get updated. And I'm not getting any sort of error. Everything is working correctly, the config items just aren't updating. But if I say $this->config->set_item('item_name', 'new_value'); on it's own, it updates how it should.

Can anyone help me?

Controller code:
Code:
function update_settings() {
            
            $this->admin_model->update_settings();
            
            redirect('admin/settings');
            
        }

Model code:
Code:
function update_settings() {
            
            $this->config->set_item('site_name', $this->input->post('site_name'));
            
        }

View code:
Code:
<?php echo form_open('admin/update_settings'); ?>

    <p>
        <label for="title">Forum title</label>
        &lt;?php echo form_input('site_name', $this->config->item('site_name')); ?&gt;
    </p>
    
    <p>
        &lt;?php echo form_submit('update', 'Update'); ?&gt;
    </p>

&lt;?php echo form_close(); ?&gt;



Updating config items through a form? - El Forum - 02-07-2011

[eluser]ReLexEd[/eluser]
$this->config->set_item() only sets the item for the current execution, not in the config-file.

If you wish to change the config-item in the file you'll have to write them to the appropriate config-file yourself.


Updating config items through a form? - El Forum - 02-07-2011

[eluser]Joshee[/eluser]
So there is no way I would be able to update the config items through a form like I am trying to do?


Updating config items through a form? - El Forum - 02-07-2011

[eluser]Twisted1919[/eluser]
You can with MY_Config:
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Config extends CI_Config{

    private $ci = NULL;

    public function __construct()
    {
        parent::__construct();
    }

    public function load_db_items()
    {
        if (is_null($this->ci)) { $this->ci =& get_instance(); }
        $result = array();
        $query = $this->ci->db->get(TBL_CONFIG);
        if($query->num_rows() > 0)
        {
            $result = $query->result();
            foreach ($result as $row)
            {
                $this->set_item($row->key, $row->value);
            }
        }
    }

    public function save_db_item($key, $value)
    {
        if (is_null($this->ci)) { $this->ci =& get_instance(); }

        $where = array('key' => $key);
        $found = $this->ci->db->get_where(TBL_CONFIG, $where, 1);

        if ($found->num_rows > 0)
        {
            return $this->ci->db->update(TBL_CONFIG, array('value' => $value), $where);
        }
        else
        {
            return $this->ci->db->insert(TBL_CONFIG, array('key' => $key, 'value' => $value));
        }        
    }

    public function remove_db_item($key)
    {
        if (is_null($this->ci)) { $this->ci =& get_instance(); }
        return $this->ci->db->delete(TBL_CONFIG, array('key' => $key));
    }
    
    public function create_table()
    {
        if (is_null($this->ci)) { $this->ci =& get_instance(); }

        $this->ci->load->dbforge();

        $this->ci->dbforge->add_field("`id` int(11) NOT NULL auto_increment");
        $this->ci->dbforge->add_field("`updated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP");
        $this->ci->dbforge->add_field("`key` varchar(255) NOT NULL");
        $this->ci->dbforge->add_field("`value` text NOT NULL");
        $this->ci->dbforge->add_key('id', TRUE);
        $this->ci->dbforge->create_table(TBL_CONFIG, TRUE);
    }    
//        
}

/* End of file MY_Config.php */
1) TBL_CONFIG is a constant, you change it with your table name.
2) Generate your database table with create_table() method;
3) When you want to save a item into database, you do save_db_item()
4) To load all the items from database, use load_db_items() in something like MY_CONTROLLER
5) Access the database items after you called load_db_items() with item() method;


Updating config items through a form? - El Forum - 02-07-2011

[eluser]Joshee[/eluser]
Thanks, that might help!


Updating config items through a form? - El Forum - 02-08-2011

[eluser]SitesByJoe[/eluser]
I've been planning to write this code but you beat me to it! Nice one on the "MY_config!"


Updating config items through a form? - El Forum - 02-08-2011

[eluser]Eric Barnes[/eluser]
Here is one I use as a library: https://github.com/ericbarnes/ci-settings


Updating config items through a form? - El Forum - 02-08-2011

[eluser]Joshee[/eluser]
[quote author="Eric Barnes" date="1297199904"]Here is one I use as a library: https://github.com/ericbarnes/ci-settings[/quote]

Thank you for that, Eric! That worked for my problem!


Updating config items through a form? - El Forum - 02-23-2011

[eluser]predat0r[/eluser]
[quote author="Eric Barnes" date="1297199904"]Here is one I use as a library: https://github.com/ericbarnes/ci-settings[/quote]

Hi, question: what is autoload in the settings lib?

Thanks