views:

152

answers:

1

I've developed a website in php codeigniter with the idea of using single physical instance of the code.

Here the logic i wanted to be is on login page user will chose the companyid which will be internally to the database name. I want to know how to update the active_group variable according to the company id chosen by the user at the time of login.

Is there any way to do so.

A: 

You will need to have a main database for the user authentication and storing the information anyway right? So you need to manage two connections. You can have your default connection in there, then set your 2nd config in MY_Controller.

This MY_Controller would handle the user detection (from the auto-loaded database class and whatever model/lib/auth system you are using) then it would of course get the database name from the user however it may be attached.

You can then use code like this to set a 2nd db:

class  MY_Controller extends Controller
{
    function MY_Controller ()  {

        parent::Controller();

        $this->user = $this->auth->get_user($this->input->post('username'), $this->input->post('password'));
        $this->company = $this->company->get($this->input->post('company_id'));

        $config['hostname'] = "localhost";
        $config['username'] = "myusername";
        $config['password'] = "mypassword";
        $config['database'] = $this->company->database;
        $config['dbdriver'] = "mysql";
        $config['dbprefix'] = "";
        $config['pconnect'] = FALSE;
        $config['db_debug'] = TRUE;
        $config['cache_on'] = FALSE;
        $config['cachedir'] = "";
        $config['char_set'] = "utf8";
        $config['dbcollat'] = "utf8_general_ci";

        $this->company_db = $this->load->database($config, TRUE);
    }

}

You can then interact with $this->company_db as well as $this->db. The former would be for db interactions with their database, the latter being for interactions with the main default connection.

Phil Sturgeon