Welcome Guest, Not a member yet? Register   Sign In
Access Database in Core class
#3

(This post was last modified: 01-07-2020, 07:39 PM by keklein. Edit Reason: Spelling )

(01-05-2020, 07:48 AM)LPena Wrote: I'm trying to make a site multilanguage.
We use diffent .json files with the translation of every term according to de language wich can be the set in a cookie or can be the default language.

I changed .htaccess file so when we get something like http://siteurl/en/controller/action it would rewrite to http://siteurl/controller/action?__lang=en, this works perfectly

The problem appeared when i had to set the new base_url, because the available languages can be eddited by an administrator, krogerfeedback so I proceed to create a new controller in the application/core folder in wich i changed the base_url with the cookie value or the default language

But i had to use a mysqli object, write user and password again, and I didnt like that but I cant use the DB because core classes instances are created long before $CI is created so no "instance" is available for &get_instance().

Any help or idea to make this cleaner?

Heres my code:

PHP Code:
class MY_Config extends CI_Config {

    public function __construct() {

        $this->config =& get_config();
        $language_code '';
        if(isset($_COOKIE['language']))
        {
            $conn = new mysqli("localhost","root","root","test");
            $stmt $conn->prepare("select * from language l where l.code = ?");
            $stmt->bind_param("s"$code);
            $code $_COOKIE['language'];
            $stmt->execute();
            $result $stmt->get_result();
            
            
if($result->num_rows == 1)
                $language_code $_COOKIE['language'];
        }
        
        
if($language_code == '')
        {
            $conn = new mysqli("localhost","root","root","test");
            $stmt $conn->prepare("select s.value from settings s where s.key = ?");
            $stmt->bind_param("s"$lang);
            $lang 'language';
            $stmt->execute();
            $result $stmt->get_result();
            if($result->num_rows == 1)
                $language_code $result->fetch_assoc()['value'];
        }

        if($language_code != '')        
            $this
->set_item('base_url'$this->config['base_url'] . $language_code '/');
    }

I have also faced same issue. Did you really find solution??
Reply


Messages In This Thread
Access Database in Core class - by LPena - 01-05-2020, 07:48 AM
RE: Access Database in Core class - by jreklund - 01-05-2020, 11:16 PM
RE: Access Database in Core class - by keklein - 01-06-2020, 10:21 AM
RE: Access Database in Core class - by customy - 04-15-2020, 03:15 AM
RE: Access Database in Core class - by ZoeF - 01-09-2020, 03:32 PM
RE: Access Database in Core class - by maxedip556 - 02-17-2020, 03:22 AM



Theme © iAndrew 2016 - Forum software by © MyBB