Welcome Guest, Not a member yet? Register   Sign In
create dynamic variable in config.php
#1

[eluser]marine[/eluser]
Hello,

I want use dynamic variable in file config.php.
It's for creating a multilingue website and I want stock all language in a database and use this information for my variable in config.php (for example for $config['language']).

I don't found how we can access on database in config.php.

Can you please help me? :-)

Marine

ps : excuse me if my english is not good but I'm french! ;-)
#2

[eluser]Eduard Stankovic[/eluser]
yes it is a possible .. try something like this in MY_controller or MY_lang.... it is up to you... but try to use i18n library Smile

Code:
global $CFG;

$CFG->set_item('language', 'slovak');

or
Code:
$this->config->set_item('language', 'slovak');

I hope it's what are you looking for Smile
#3

[eluser]marine[/eluser]
Thank you for your response. But it's not really that.

I explain more :

in my file application/config/config.php I have some language variable :

/* default language */
$config['language'] = "french";

/* default language abbreviation */
$config['language_abbr'] = "fr";

/* set available language abbreviations */
$config['lang_uri_abbr'] = array("en" => "english", "fr" => "french");

/* hide the language segment (use cookie) */
$config['lang_ignore'] = FALSE;

I want stock my language information in database for edit and add language from back office.
So, when I edit or add an information language, I need see this language in $config['lang_uri_abbr'].
So I want define $config['lang_uri_abbr'] in config.php with database information.
But if I use $ci = &get;_instance() it's doesn't work...
So I don't know how access database from config.php.

Are you understand?

Thanks again for your help! :-)
#4

[eluser]CroNiX[/eluser]
You can't because config loads very early on in the bootstrap process and the db object isn't available yet. See /core/CodeIgniter.php to see how and the order that the CI objects are loaded/used.

I would create a base controller, MY_Controller, and set your lang def there in the __construct(). Then have all of your other controllers extend that base controller instead of CI_Controller so it will be available to everything (including your database).

You also might be able to using a hook, but I think that would be pretty inefficient for this.
#5

[eluser]marine[/eluser]
Hello,

thank you for your help. I try to modified variable from MY_Controller but it don't function.
I found an other solution, not the best I imagine but this solution function for the moment!

I connect database with php like :
Code:
mysql_connect("server", "login", "password"); // Connexion à MySQL
mysql_select_db("my_database"); // Sélection de la base test
$query = "SELECT libelle, code FROM langues WHERE active = 1";
$result = mysql_query($query);
$lg = array();
while($row = mysql_fetch_array($result)){
    $lg[$row['code']] = $row['libelle'];
}
mysql_close();

$config['lang_uri_abbr'] = $lg;

thanks again for your help.




Theme © iAndrew 2016 - Forum software by © MyBB