• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Access Database in Core class

#6
(01-06-2020, 10:21 AM)keklein Wrote:
(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, 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().

hey, have you done it successfully, i'm working on client site and struggling to do it? does this work?

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

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.