Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write an array to config in Codeigniter?

I'm working with a config file that I have create in order to store users. This surely wasn't what the configs were intended to be used for, but it's an extremely small application and I think that it would be a nice solution.

My array looks like this:

$config['users'] = array(array('username' => 'username', 'password' => 'password'));

This works well. I can retrieve the information quick and easy. BUT, if I try to write a new array (a new user) to the config file I get this error: Illegal offset type in isset or empty

I'm using $this->config->item('users', array('username' =>....)) which doesn't appear to support arrays.

How can I write arrays to my config variable? Is there another way?

EDIT: Alright, the error is fixed thanks to the answer given by phirschy. I was so sure in my head that I could use config->item() that I didn't check the manual for a config->set_item()... BUT, it still doesn't work. Here is the specific code:

        $users = $this->config->item('users');
        array_push($users, array('username' => $this->input->post('username'), 'password' => $this->input->post('password')));
        $this->config->set_item('users', json_encode($users));
        echo json_encode($users);

This code is called via Ajax, and I have an alert box to see if it the values are correct. They are. And as you can see, I've tried storing it as json instead of array as well.... but that doesn't work either. Help please?

thank you

like image 280
C. E. Avatar asked Aug 01 '10 17:08

C. E.


1 Answers

You have to use the 'set_item' method to write a config item, not 'item':

$this->config->set_item('item_name', 'item_value');

Or in your case:

$this->config->set_item('users', array(...));
like image 104
phirschybar Avatar answered Sep 19 '22 12:09

phirschybar