Welcome Guest, Not a member yet? Register   Sign In
Connecting to database from config
#1

[eluser]blasto333[/eluser]
Code:
if(isset($_SERVER['HTTP_HOST']))
{
    $config['base_url'] = isset($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) == 'on' ? 'https' : 'http';
    $config['base_url'] .= '://'. $_SERVER['HTTP_HOST'];
    $config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']), '', $_SERVER['SCRIPT_NAME']);
}

else
{
    $config['base_url'] = 'http://localhost/';
}
/*
|--------------------------------------------------------------------------
| Company ID
|--------------------------------------------------------------------------
|
| Company ID for this installation. LOOKUP IN DATABASE TABLE to determine company_id
|
|    http://example.com/
|
*/
$config['company_id'] = 1;

How can I use code ignitor to connect to the database from a config file?
#2

[eluser]stuffradio[/eluser]
Why not just use the database.php file in the config folder?
#3

[eluser]theprodigy[/eluser]
I would assume, the same way you would with custom libraries

Code:
$CI =& get_instance();
$CI->load->model('whatever');

or the old fashioned PHP way

Code:
$link = mysql_connect( $server, $username, $password )
mysql_select_db( $database_name, $link )
#4

[eluser]Colin Williams[/eluser]
Well, you don't want to hide data-related functionality in a config file. It should be in a model. So, do this:

1. Use config files for default values.
2. In the Controller's constructor (or a hook), use a model to get custom settings, iterate over the result, and use the config class methods to update the values.

Quote:I would assume it works the same way as with custom libraries

I'm pretty certain the Config class is loaded before the controller, so no, that would not work. get_instance() is only effective (and available) at a certain point in the execution of the framework




Theme © iAndrew 2016 - Forum software by © MyBB