Radenko Kosic
Radenko Kosic

Reputation: 13

Use existing MySQL database in CodeIgniter

In my PHP websites I’m using SiteTranslator script for a website translated into 30 languages. Each translation is stored in its own table (text_en, text_de...) and each table has 3 columns (textKey, textValue, lastUpdate).

Now I would like to use that database in my CodeIgniter application. What would be the best way to do it?

Upvotes: 1

Views: 458

Answers (1)

Philip
Philip

Reputation: 4592

You could use multiple databases as suggested, you would still need to setup your app language files

{read more in the user guide}

Based on the first uri segment you could try something like this.

Adding routes

$route['en|fr|gr/test'] = 'test';

First segment checks for en OR fr OR whatever else.

Then the Main Controller catches the first segment before the test controller is initialized and the db(object) && app(language) files are set

www.site.com/en/test => load english language file(application/language/english/mylanguage) and db

www.site.com/fr/test => load french language file(application/language/french/mylanguage) and db ...and so on

Main Controller

class MY_Controller extends CI_Controller{

    protected $lang, $db;

    public function __construct(){
        parent::__construct();

        $this->set_language();
    }

    protected function set_language(){
        switch($this->uri->segment(1))
        {
            case 'en':
                $this->lang     = $this->lang->load('mylanguage', 'english');
                $this->db       = $this->load->database('en', TRUE); 
            break;

            case 'fr':
                $this->lang     = $this->lang->load('mylanguage', 'french');
                $this->db       = $this->load->database('fr', TRUE); 
            break;

            default:
                $this->lang     = $this->lang->load('mylanguage', 'english');
                $this->db       = $this->load->database('en', TRUE); 
            break;
        }
    }


}

Upvotes: 1

Related Questions