CodeIgniter Forums

Full Version: How can I change an array element in the $config array from a Controller?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]warpspasm[/eluser]
Good Morning!

In application/config/my.php

I have a

$config['my_element'] = 'abcdef';

From a controller in /application/controllers/my_cont.php

function mine()
{
// I'd need to set $config['my_element'] again
// I tried this but it did not work:

global $config;

$config['my_element'] = 'ghijk';

// But it did not work, $config['me_element'] stayed the original value in another function!

}

How can I do this?
Thank you

El Forum

El Forum

[eluser]memVN[/eluser]
You should code as:
In controller: $this->config->set_item('your_element', 'new value');

Good luck

El Forum

[eluser]CroNiX[/eluser]
Changing config values via the config library does NOT write the data to the config file. It will only be changed for that request (in memory) and then get set back to the original value when the config library gets reinitialized and reloads the config files on the next request.

If you need a permanent change, or to pass the change to another server request, you'll have to extend the config library so it will write changes to the config.php file, or use sessions, or use a cookie, or create a config system using the database, or.... but the config library won't make permanent changes.